$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>metabox - Using meta boxes as the title of a custom post type|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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...
}
Post a comment

comment list (0)

  1. No comments so far