Setting Default Category for Custom Post Type Upon Autosave

Use the save_post action hook and in the call back function use wp_set_object_terms( $object_id, $terms, $taxonomy, $append ) function.

For your custom post type the code can be like this

function save_book_meta( $post_id, $post, $update ) {

    $slug = 'book'; //Slug of CPT

    // If this isn't a 'book' post, don't update it.
    if ( $slug != $post->post_type ) {
        return;
    }

    wp_set_object_terms( get_the_ID(), $term_id, $taxonomy );
}

add_action( 'save_post', 'save_book_meta', 10, 3 );

$taxonomy – The context in which to relate the term to the object. This can be category, post_tag, or the name of another taxonomy.

$term_id – term ID of the taxonomy

I do not know your project thoroughly, so you can consider this snippet as a way of doing what you wanted to do.

For more reference visit this two links given below :

https://codex.wordpress.org/Function_Reference/wp_set_object_terms

https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

I hope you will find a way out.

Leave a Comment

error code: 521