metabox - Using meta boxes as the title of a custom post type

admin2025-06-06  10

I have a custom post type with some meta boxes added to the post type. The code for the CPT meta boxes works fine. I then added the following code to use the meta boxes as the title of the post. The problem is that the code works for all posts, pages and other CPT on the site.

Could someone please advise on how to change below code to just work for the CPT it is attended to be used for.

////////////////////////////////////////////////////////////////////
// Set post title //
////////////////////////////////////////////////////////////////////   
add_action( 'save_post', 'post_updated' );
function post_updated( $post_id ) {
$meta_box_one       = get_post_meta(get_the_ID(), 'meta_box_one', true);
$meta_box_two       = get_post_meta(get_the_ID(), 'meta_box_two', true);
$meta_bax_three     = get_post_meta(get_the_ID(), 'meta_bax_three', true);
$post_name  = '' . $meta_box_one . ' ' . $meta_box_two  . ' ' . 
$meta_box_three . '';

   // verify post is not a revision & not an autosave
   if ( !wp_is_post_revision(  $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
    // set the new post title
    $post['ID'] = $post_id;
    $post['post_title'] =  $post_name; 

    // update the post, removing the action to prevent an infinite loop
    remove_action( 'save_post', 'post_updated' );
    wp_update_post($post);
    add_action( 'save_post', 'post_updated' );
}
}

I have a custom post type with some meta boxes added to the post type. The code for the CPT meta boxes works fine. I then added the following code to use the meta boxes as the title of the post. The problem is that the code works for all posts, pages and other CPT on the site.

Could someone please advise on how to change below code to just work for the CPT it is attended to be used for.

////////////////////////////////////////////////////////////////////
// Set post title //
////////////////////////////////////////////////////////////////////   
add_action( 'save_post', 'post_updated' );
function post_updated( $post_id ) {
$meta_box_one       = get_post_meta(get_the_ID(), 'meta_box_one', true);
$meta_box_two       = get_post_meta(get_the_ID(), 'meta_box_two', true);
$meta_bax_three     = get_post_meta(get_the_ID(), 'meta_bax_three', true);
$post_name  = '' . $meta_box_one . ' ' . $meta_box_two  . ' ' . 
$meta_box_three . '';

   // verify post is not a revision & not an autosave
   if ( !wp_is_post_revision(  $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
    // set the new post title
    $post['ID'] = $post_id;
    $post['post_title'] =  $post_name; 

    // update the post, removing the action to prevent an infinite loop
    remove_action( 'save_post', 'post_updated' );
    wp_update_post($post);
    add_action( 'save_post', 'post_updated' );
}
}
Share Improve this question edited Nov 14, 2018 at 6:37 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Nov 14, 2018 at 6:33 MFWebMasterMFWebMaster 114 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can use get_post_type to check type:

function post_updated( $post_id ) {
    $post_type = get_post_type($post_id);
    // If this isn't a 'book' post, don't update it.
    if ( "book" != $post_type ) return;

    // the rest of your code...
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749183594a317060.html

最新回复(0)