Change Title to Custom field using "rwmb__before_save_post" action.
- This topic has 7 replies, 3 voices, and was last updated 2 years, 7 months ago by
Long Nguyen.
-
AuthorPosts
-
August 20, 2021 at 3:07 AM #30350
Jeremy Parrott
ParticipantI want to make the Post Title the same value as one of the fields in my "Monitor" CPT automagically and I have 3 questions on how to do this.
Q1) To do this I thought I could use the rwmb_before_save_post (or rwmb_{$meta_box_id}_before_save_post) action but I'm not getting the syntax right because it doesn't even appear to be getting to my code. I have this test code in my functions.php file:
function update_post_title( $object_id ) { die("I got here!"); //var_dump($object_id); }; add_action( 'rwmb_before_save_post', 'update_post_title', 10, 1 );
Q2) Once my function is called successfully, how do I make the Post Title the same as my 'Monitor ID' field (I need this to link 2 metaboxes together)?
Q3) I think I should be using rwmb_{$meta_box_id}_before_save_post but I'm not sure where to find the $meta_box_id. Is it the CPT "post=14" number, the Field Group "post=13" number, or something else?
August 20, 2021 at 12:49 PM #30354Long Nguyen
ModeratorHi Jeremy,
Please refer to this topic https://support.metabox.io/topic/adding-metabox-field-data-to-the-post-title/
to know how to update the post title by using the custom field's value.
August 20, 2021 at 9:29 PM #30361Jeremy Parrott
ParticipantI've added the following code to my functions.php but it is not updating the title.
add_action( 'rwmb_monitor_after_save_post', 'update_post_title' ); function update_post_title( $post_id ) { // Get the field value $my_meta = rwmb_meta( 'monitor_number', '', $post_id ); // Get the post title //$my_post_title = get_the_title( $post_id ); NOT NEEDED - I'M OVERWRITING THE TITLE // Preprare update post $my_post = array( 'ID' => $post_id, 'post_title' => $my_meta, ); wp_update_post( $my_post ); }
My CPT slug is "monitor" and my field slug is "monitor_number".
What am I doing wrong?
August 21, 2021 at 9:25 PM #30369Long Nguyen
ModeratorHi,
You need to change the CPT slug to the meta box ID. If you are using the Builder, it is the Field Group ID, screenshot https://share.getcloudapp.com/nOuvkr6A
So the sample action hook would be
add_action( 'rwmb_post-meta_after_save_post', 'update_post_title' );
August 25, 2021 at 5:00 AM #30413Jeremy Parrott
ParticipantThanks, I got it working. The screenshot helped a lot!
October 8, 2022 at 4:17 AM #38590ScottN
ParticipantLong, Hope you are well.
Suppose I wanted to customize the slug on save (from frontend submission form) in this same way, what would I place here (would it be post_slug)?:
(from your code above in this thread):
add_action( 'rwmb_monitor_after_save_post', 'update_post_title' ); function update_post_title( $post_id ) { // Get the field value $my_meta = rwmb_meta( 'monitor_number', '', $post_id ); // Get the post title //$my_post_title = get_the_title( $post_id ); NOT NEEDED - I'M OVERWRITING THE TITLE // Preprare update post $my_post = array( 'ID' => $post_id, 'post_slug' => $my_meta, ); wp_update_post( $my_post ); }
October 8, 2022 at 5:33 AM #38591ScottN
Participant@Long, here is my finished code for above^. Is it post_name I need to update?:
<?php add_action( 'rwmb_distributor-details_after_save_post', 'update_post_slug' ); function update_post_slug( $post_id ) { // Get the street_address field value $dist_address = rwmb_meta( 'street_address', '', $post_id ); // Get the locality field value $dist_city = rwmb_meta( 'locality', '', $post_id ); // Get the administrative_area_level_1 field value $dist_state = rwmb_meta( 'administrative_area_level_1', '', $post_id ); // Get the post title $dist_post_title = get_the_title( $post_id ); // Preprare update post slug (post_name) $my_post = array( 'ID' => $post_id, 'post_name' => $dist_post_title . '-' . $dist_address . '-' . $dist_city . '-' . $dist_state, ); wp_update_post( $my_post ); }
October 8, 2022 at 8:54 PM #38596Long Nguyen
ModeratorHi Scott,
Yes, correctly. Using the argument
post_name
to update the post slug. Read more on the documentation https://developer.wordpress.org/reference/classes/wp_post/ -
AuthorPosts
- You must be logged in to reply to this topic.