Support Forum
Hi,
When I publish a post, I would to add to the post title, the version of the document (metabox 'version').
How can I add the data from the 'versionning' field to the post title ?
Like this : POST_TITLE 'version'
$meta_boxes[] = [
'title' => esc_html__( 'Version', 'text-domain' ),
'id' => 'versionning',
'post_types' => ['docs'],
'context' => 'side',
'priority' => 'high',
'fields' => [
[
'id' => $prefix . 'version',
'name' => "Version",
'type' => 'text',
'step' => "0.01",
'required' => 1,
],
],
];
Hi Ben,
You can use the action rwmb_{$meta_box_id}_after_save_post and the function wp_update_post() to update the post title after saving a specific meta box.
Here is the sample code:
add_action( 'rwmb_versionning_after_save_post', 'update_post_title' );
function update_post_title( $post_id ) {
// Get the field value
$my_meta = rwmb_meta( 'version', '', $post_id );
// Get the post title
$my_post_title = get_the_title( $post_id );
// Preprare update post
$my_post = array(
'ID' => $post_id,
'post_title' => $my_post_title . ' ' . $my_meta,
);
wp_update_post( $my_post );
}
Thank you it works well!
Where can I find $meta_box_id ?
Hi Alperen,
It's the field group ID if you use the Builder, please follow this documentation to get more details https://docs.metabox.io/creating-meta-boxes/
Hi Long,
I am also trying to do this. But I need some help with the above sample code and documentation. Can you possibly indicate which elements I need to customise and with exactly what. It is really helpful when you can add //update this variable with your meta-box-id. I am very new to this and so nothing is obvious to me.
I will also need to do this for post excerpt and post content...
Many thanks!
Yasmine
Hi Yasmine,
If you are using the builder, you can share the code that creates meta box (field group) and custom fields on your site. I will help you to indicate the meta box ID.
https://docs.metabox.io/extensions/meta-box-builder/#getting-php-code
Hi Long,
I just saw your reply. Thank you. Here is my PHP:
https://pastebin.com/BUunL5vK
Hi Yasmine,
If you want to update the post title by the field Research Title
value, here is the code:
add_action( 'rwmb_research-summary_after_save_post', 'update_post_title' );
function update_post_title( $post_id ) {
// Get the field value
$my_meta = rwmb_meta( 'academicpost_title', '', $post_id ); //include the $prefix academic to the field ID
// Preprare update post
$my_post = array(
'ID' => $post_id,
'post_title' => $my_meta,
);
wp_update_post( $my_post );
}
Thank you again Long.
And would it be the same logic for post_content? And excerpt? Eg:
add_action( 'rwmb_research-summary_after_save_post', 'update_post_content' );
function update_post_title( $post_id ) {
// Get the field value
$my_meta = rwmb_meta( 'academicpost_content', '', $post_id );
// Prepare update post
$my_post = array(
'ID' => $post_id,
'post_content' => $my_meta,
);
wp_update_post( $my_post );
}
Hi,
Yes, it will work with post_content or other post elements. Please read more on the documentation https://developer.wordpress.org/reference/functions/wp_insert_post/
Thanks ... I don't understand how to apply the documentation - hence my question... Is this correct?
add_action( 'rwmb_research-summary_after_save_post', 'update_post_content' );
function update_post_content( $post_id ) {
$my_meta = rwmb_meta( 'academicpost_content', '', $post_id );
$my_post = array(
'ID' => $post_id,
'post_content' => $my_meta,
);
wp_update_post( $my_post );
}
Yes, it is correct. Can you please confirm it works?
If I wanted to combine two field_id's to create a custom title how would I go about merging them? For instance, I have first_name and then last_name. I want both of these to show in post_title together.
Also can that be applied to rewrite the slug of the post? Right now it displays as auto-draft.