Custom Fields
Re-Envisioned
Support General Change Title to Custom field using "rwmb__before_save_post" action.

  • Creator
    Topic
  • #30350
    Resolved Jeremy Parrott
    Participant

    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?

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

    Hi Jeremy,

    Please refer to this topic https://metabox.io/support/topic/adding-metabox-field-data-to-the-post-title/

    to know how to update the post title by using the custom field's value.

    #30361
    Jeremy Parrott
    Participant

    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?

    #30369
    Long Nguyen
    Moderator

    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' );
    
    #30413
    Jeremy Parrott
    Participant

    Thanks, I got it working. The screenshot helped a lot!

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