Setting Default Category for Custom Post Type Upon Autosave

All we need is an easy explanation of the problem, so here it is.

I’ve been searching for a couple hours and consulted multiple posts, but I seem to get this to work.

I have this weird bug where users cannot see their drafts of a custom post type if they don’t assign a category to them. So if they just click ‘save as draft’ and want to go back to it later, it won’t be visible to them (I have to go in as an admin and set a category for it to be visible to them). I have no idea why this is occurring but I’m willing to work around it.

In Settings > Writing you can set the default category for a regular post, but there is no such option available for a custom post type. I am fine to set the default type to ‘Uncategorized’, just like it is set with regular posts. So I’m trying to accomplish this.

Some snippets I’ve come across like this are aimed for ‘default category upon publish’ but I need it upon autosave (some users just have access to ‘save draft’ and ‘submit for publishing’). At least 6 I’ve come across are unanswered.

I did implement one specific code unsuccessfully (I can’t find the snippet for the life of me, but the desired default category used in the example was ‘authors’). This is driving me nuts, and I would really appreciate your help. Thanks.

EDIT: I have tried the following code (which I got from here) and got ‘uncategorized’ to be checked automatically upon save for the post type ‘community’ but the problem is that it completely overrides other categories you might replace this with. That is to say, if you uncheck ‘uncategorized’ and choose meaningful categories, upon ‘publish’ or ‘save’, all of those selections are erased and it reverts back to community. Need it to just have ‘uncategorized’ until the user replaces that default category (exactly the way a defaul category works with regular ‘post’ type).

function add_comm_category_automatically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
$uncategorized= array (1);
wp_set_object_terms( $post_ID, $uncategorized, 'category');
}
}
add_action('save_post', 'add_comm_category_automatically');

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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.

Method 2

I am using pods.io to build my CPTs and custom taxonomies. Had the same problem. With the code from @mishu, I was able to accomplish my goal.

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

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

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

    // Get the ID of default/ fallback category
    // $default_term = get_term_by('slug', 'your_term_slug', 'your_custom_taxonomy');

    $default_term = get_term_by('slug', 'alle', 'termin_cat');

    wp_set_object_terms( get_the_ID(), $default_term->term_id, 'termin_cat' );
}

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

Method 3

If I understand correctly, all you need to do is to add the optional parameter $append as true (the default is false)

wp_set_object_terms( int $object_id, string|int|array $terms, string $taxonomy, bool $append = false )

for example, in the code you posted:
wp_set_object_terms( $post_ID, $uncategorized, ‘category’, true);

that worked for me.

Method 4

It is possible to do it this way. If other answers didn’t work, you can try this:

function auto_category( $post_id, $post, $update ) {
    
    $slug = 'news'; //Slug of CPT
    
    // If this isn't the right slug, don't update it.
    if ( $slug != $post->post_type ) {
        return;
    }
    
    $category_term = get_term_by( 'name', 'news_category_slug', 'category' );
    
    wp_set_object_terms( get_the_ID(), $category_term->term_id, 'category' );
}

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

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply