Support Forum
I 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?
Hi 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.
I'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?
Hi,
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' );
Thanks, I got it working. The screenshot helped a lot!
Long, 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 );
}
@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 );
}
Hi 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/