admin管理员组文章数量:1130349
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
1 Answer
Reset to default 0You 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...
}
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
1 Answer
Reset to default 0You 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...
}
本文标签: metaboxUsing meta boxes as the title of a custom post type
版权声明:本文标题:metabox - Using meta boxes as the title of a custom post type 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749183570a2329014.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论