Custom Fields
Re-Envisioned
Support General Adding metabox field data to the post title

  • Creator
    Topic
  • #24622
    Resolved ben06
    Participant

    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,
    ],
    ],
    ];

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #24634
    Long Nguyen
    Moderator

    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 );
    }
    #24640
    ben06
    Participant

    Thank you it works well!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.