Adding metabox field data to the post title
- This topic has 13 replies, 5 voices, and was last updated 2 years, 4 months ago by
Solace.
-
AuthorPosts
-
February 25, 2021 at 2:42 AM #24622
ben06
ParticipantHi,
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,
],
],
];February 25, 2021 at 12:48 PM #24634Long Nguyen
ModeratorHi 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 ); }
February 25, 2021 at 6:41 PM #24640ben06
ParticipantThank you it works well!
January 6, 2022 at 2:48 AM #33021Alperen Ozkan
ParticipantWhere can I find $meta_box_id ?
January 6, 2022 at 8:11 AM #33030Long Nguyen
ModeratorHi 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/
February 22, 2022 at 1:13 AM #34045Yasmine
ParticipantHi 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!
YasmineFebruary 23, 2022 at 5:28 PM #34073Long Nguyen
ModeratorHi 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-codeFebruary 24, 2022 at 8:04 PM #34107Yasmine
ParticipantHi Long,
I just saw your reply. Thank you. Here is my PHP:
https://pastebin.com/BUunL5vKFebruary 24, 2022 at 10:29 PM #34115Long Nguyen
ModeratorHi 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 ); }
February 24, 2022 at 11:14 PM #34119Yasmine
ParticipantThank 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 ); }
February 25, 2022 at 9:28 AM #34126Long Nguyen
ModeratorHi,
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/
February 25, 2022 at 2:04 PM #34133Yasmine
ParticipantThanks ... 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 ); }
February 26, 2022 at 10:01 AM #34147Long Nguyen
ModeratorYes, it is correct. Can you please confirm it works?
December 9, 2022 at 11:42 AM #39634Solace
ParticipantIf 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.
-
AuthorPosts
- You must be logged in to reply to this topic.