Quantcast
Channel: Question and Answer » default
Viewing all articles
Browse latest Browse all 17

Setting Default Category for Custom Post Type Upon Autosave

$
0
0

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');

Viewing all articles
Browse latest Browse all 17

Trending Articles