4

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

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.

  • thank you for your answer. as i said in the main post, i would want the category to be 'uncategorized' and the post type is called 'community'. however, critically, i want it so that whenever 'publish', 'update', 'save draft', 'submit for review' or anything else is pressed, the NEW categories the user chooses to replace the 'uncategorized' are not overridden (as is the case with the code I just added in the main post). – zk87 May 28 '15 at 15:53
  • I tried several iterations of the code you suggested, but I couldn't get anything to work. The one I thought had the best chance of working was: function save_community_meta( $post_id, $post, $update ) { $slug = 'community'; //Slug of CPT // If this isn't a 'community' post, don't update it. if ( $community != $post->post_type ) { return; } wp_set_object_terms( get_the_ID(), $category, $uncategorized); } add_action( 'save_community', 'save_community_meta', 10, 3 ); – zk87 May 28 '15 at 15:54
  • (sorry for the jumbled mess... i can't seem to format categories as neatly as posts) – zk87 May 28 '15 at 15:56
  • What are the values you are passing as $category and $uncategoried? – mishu May 29 '15 at 23:32
  • wp_set_opject_terms( get_the_ID(), '1', 'category' ); You can use this code for default Uncategorized category of post. – mishu May 29 '15 at 23:39
0

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 );
  • Is there any way to do this before autosave? so a default category is selected at post creation? – Shae Mar 12 at 12:34
  • Not in my knowledge. If you write a standard-post, then the cat-ID 1 will be selected after saving the post, not before. – BrainBUG Mar 24 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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