2

I currently have a custom meta field for adding a video url to the post. I'd like for the existing taxonomy term "video" to be automatically assigned to the post upon saving if the meta field has any value.

1 Answer 1

4

You should hook onto the save_post action.

add_action( 'save_post', 'add_video_taxonomy' );

function add_video_taxonomy( $post_id ) {

    // you should check if the current user can do this, probably against publish_posts
    // you should also have a nonce check here as well

    if( get_post_meta( $post_id, 'video_url', true ) ) {
        wp_set_post_terms( $post_id, 'video', 'your_custom_taxonomy_name', true );
    }

}
1
  • Thanks! This worked well. I didn't mention this above but in case anyone else needs it I had to use 'wp_set_object_terms' instead since the post in question is a custom post type.
    – rspny
    Commented Dec 13, 2013 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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