Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

2 Answers 2

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.org/Plugin_API

share|improve this answer
    
I do this: 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 doesn´t work. To me, the code looks fine. I don´t know what I´m doing wrong. –  TheMadcore Feb 18 '13 at 5:24

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?

share|improve this answer
    
Yes there is see this article wphub.com/custom-post-type-publish-action-hook –  Anagio Feb 18 '13 at 13:31
    
Great! Works like a charm. Thanks a lot! –  TheMadcore Feb 18 '13 at 14:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.