admin管理员组文章数量:1130349
I'm trying to figure out how to make a function to automatically add a "featured" tag to a post, based on a checkbox in a metabox I've added to the edit screen.
I think the function I need to use is wp_set_object_terms, but I don't get how it works. I know how to check if those checkboxes are active or not, but that´s all.
I hope someone can put me on the right track with this.
I'm trying to figure out how to make a function to automatically add a "featured" tag to a post, based on a checkbox in a metabox I've added to the edit screen.
I think the function I need to use is wp_set_object_terms, but I don't get how it works. I know how to check if those checkboxes are active or not, but that´s all.
I hope someone can put me on the right track with this.
Share Improve this question edited Feb 17, 2013 at 10:57 Lea Cohen 1,6603 gold badges22 silver badges45 bronze badges asked Feb 17, 2013 at 7:13 TheMadcoreTheMadcore 314 bronze badges3 Answers
Reset to default 2I accidentally found this question searching for something similar, and I liked your approach, so I improved your code a bit.
- I updated the action hook so it's being triggered on save AND update.
- Added the
wp_remove_object_termsto be able to switch back ( toggle ) custom meta box value ( in this case check-box value ).
function works fine with both, regular and custom post types, also can be used with tags, categories or custom taxonomies too.
function set_term( $post_id, $your_term ){
$post_id = get_the_ID();
$your_term = get_post_meta( $post_id, 'your_custom_meta_id', true );
// check the custom meta-box checkbox value
if ( $your_term == '1' ) {
// Create a new term if checked
wp_set_object_terms( $post_id, 'YourTerm', 'your_custom_taxonomy', true );
} else {
// Remove the created term if unchecked
wp_remove_object_terms( $post_id, 'YourTerm', 'your_custom_taxonomy' );
}
}
add_action( 'save_post', 'set_term', 10, 3 );
Ok, found the problem.
This code works like a charm:
function is_featured_post(){
$postid = get_the_ID();
$featured = get_post_meta($postid, 'wpcf-slider-if', true); if ( $featured == 1 ) {
wp_set_object_terms( $postid, 'Destacado', 'post_tag', true );
}
}
add_action ( 'publish_post', 'is_featured_post' );
But it only works in standard post, not in custom post types. There is a hook to do the same with custom post types?
You can hook into the action with add_action ( 'publish_post', 'your_function' );
Write a function to check if the checkbox is checked and if so update the posts term
http://codex.wordpress/Plugin_API
I'm trying to figure out how to make a function to automatically add a "featured" tag to a post, based on a checkbox in a metabox I've added to the edit screen.
I think the function I need to use is wp_set_object_terms, but I don't get how it works. I know how to check if those checkboxes are active or not, but that´s all.
I hope someone can put me on the right track with this.
I'm trying to figure out how to make a function to automatically add a "featured" tag to a post, based on a checkbox in a metabox I've added to the edit screen.
I think the function I need to use is wp_set_object_terms, but I don't get how it works. I know how to check if those checkboxes are active or not, but that´s all.
I hope someone can put me on the right track with this.
Share Improve this question edited Feb 17, 2013 at 10:57 Lea Cohen 1,6603 gold badges22 silver badges45 bronze badges asked Feb 17, 2013 at 7:13 TheMadcoreTheMadcore 314 bronze badges3 Answers
Reset to default 2I accidentally found this question searching for something similar, and I liked your approach, so I improved your code a bit.
- I updated the action hook so it's being triggered on save AND update.
- Added the
wp_remove_object_termsto be able to switch back ( toggle ) custom meta box value ( in this case check-box value ).
function works fine with both, regular and custom post types, also can be used with tags, categories or custom taxonomies too.
function set_term( $post_id, $your_term ){
$post_id = get_the_ID();
$your_term = get_post_meta( $post_id, 'your_custom_meta_id', true );
// check the custom meta-box checkbox value
if ( $your_term == '1' ) {
// Create a new term if checked
wp_set_object_terms( $post_id, 'YourTerm', 'your_custom_taxonomy', true );
} else {
// Remove the created term if unchecked
wp_remove_object_terms( $post_id, 'YourTerm', 'your_custom_taxonomy' );
}
}
add_action( 'save_post', 'set_term', 10, 3 );
Ok, found the problem.
This code works like a charm:
function is_featured_post(){
$postid = get_the_ID();
$featured = get_post_meta($postid, 'wpcf-slider-if', true); if ( $featured == 1 ) {
wp_set_object_terms( $postid, 'Destacado', 'post_tag', true );
}
}
add_action ( 'publish_post', 'is_featured_post' );
But it only works in standard post, not in custom post types. There is a hook to do the same with custom post types?
You can hook into the action with add_action ( 'publish_post', 'your_function' );
Write a function to check if the checkbox is checked and if so update the posts term
http://codex.wordpress/Plugin_API
本文标签: metaboxAutomatically add a tag according to custom metadata
版权声明:本文标题:metabox - Automatically add a tag according to custom metadata 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749217643a2334429.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论