Changing the Post Title/Slug after front-end submissions based on fields
Support › MB Frontend Submission › Changing the Post Title/Slug after front-end submissions based on fieldsResolved
- This topic has 4 replies, 2 voices, and was last updated 2 years, 7 months ago by
AMX.
-
AuthorPosts
-
March 28, 2023 at 9:53 AM #41235
AMX
ParticipantHi,
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,
TomMarch 28, 2023 at 6:49 PM #41242Peter
ModeratorHello,
Follow the documentation, you can get the post type and post ID from the
$objectvariable. 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.
March 28, 2023 at 7:41 PM #41247AMX
ParticipantThank 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,
TomMarch 29, 2023 at 7:35 PM #41260Peter
ModeratorHello,
Did you add the post type attribute to the frontend submission shortcode to create a new post of the
catspost 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-formMarch 29, 2023 at 11:54 PM #41265AMX
ParticipantYes, 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 -
AuthorPosts
- You must be logged in to reply to this topic.