Support Forum
Support › MB Frontend Submission › Changing the Post Title/Slug after front-end submissions based on fieldsResolved
Hi,
I have a function changing the post title and slug after saving a post. It works fine in the editor, but I would like to make it work in front-end submissions. The function is:
add_action( 'rwmb_cat-and-dog-fields_after_save_post', 'update_cats_post_title');
function update_cats_post_title( $post_id ) {
if ( 'cats' == get_post_type() ) {
$animals_name = rwmb_meta( 'animals_name', '', $post_id );
$name_sequence = rwmb_meta( 'name_sequence', '', $post_id );
$author_id = get_post_field( 'post_author', $post_id );
$nickname = get_the_author_meta( 'display_name', $author_id );
$post_slug = sanitize_title_with_dashes ($my_post,'','save');
$post_slugsan = sanitize_title($post_slug);
$my_post = array(
'ID' => $post_id,
'post_title' => $nickname . ' - Cat - ' . $animals_name . ' ' . $name_sequence ,
'post_name' => $post_slugsan,
);
wp_update_post ( $my_post ) ;}
}
I know in front-end submission I must use 'rwmb_frontend_after_save_post', but I don't know how to make it work with the above function, my attempts to integrate them failed. I will greatly appreciate your assistance.
Greetings,
Tom
Hello,
Follow the documentation, you can get the post type and post ID from the $object
variable. For example:
add_action( 'rwmb_frontend_after_save_post', 'update_cats_post_title');
function update_cats_post_title( $object ) {
$post_type = $object->post_type;
$post_id = $object->post_id;
if ( 'cats' == $post_type ) {
...
}
}
https://docs.metabox.io/extensions/mb-frontend-submission/#post-actions
Let me know how it goes.
Thank you. It works, but only if I remove the conditional if ( 'cats' == $post_type ) {...}
. With the conditional nothing happens. How can I create a conditional for the post type of the submission inside that function? I need to apply it only to selected post types.
Greetings,
Tom
Hello,
Did you add the post type attribute to the frontend submission shortcode to create a new post of the cats
post type? If yes and the code above does not work, please share the shortcode that you are using on your site.
Read more on the documentation https://docs.metabox.io/extensions/mb-frontend-submission/#submission-form
Yes, the shortcode includes post_type='cats'. If I remember correctly, the posts were not saved without that attribute.
But I have now changed the conditional to if ( 'cats' == $post_type )
and as far as I can tell, it is working now. Thanks.
Greetings,
Tom