1

I assign automatically a category to a post when published. But how can i modify this category in admin?

function add_bookcategory_automatically($post_ID) {
    global $wpdb;
    if(!wp_is_post_revision($post_ID)) {
    $bookcat = array (4 );
    wp_set_object_terms( $post_ID, $bookcat, 'category');
    }
}
add_action('publish_post', 'add_bookcategory_automatically');

For example, now the new post has category with id 4, but i can't change or add category in admin. Every time i save the post, category back to 4 id. How can i modify function?

1 Answer 1

1

Well, what is the expected behavior? You use wp_set_object_terms and pass just a single term.

If you want to make sure the specified term is assigned to the post, while being able to add as many other terms as you like, try it like this:

function add_bookcategory_automatically( $post_id ) {

    if ( ! wp_is_post_revision( $post_id ) ) {
        $bookcat = 4;
        wp_add_object_terms( $post_id, $bookcat, 'category');
    }
}
add_action( 'publish_post', 'add_bookcategory_automatically' );

See wp_add_object_terms.

0

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.