AnswerBun.com

Setting Default Category for Custom Post Type Upon Autosave

WordPress Development Asked by zk87 on December 13, 2020

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

3 Answers

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.

Correct answer by mishu on December 13, 2020

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.

Answered by izikz on December 13, 2020

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

Answered by BrainBUG on December 13, 2020

Add your own answers!

Related Questions

How to make repeated component/block in Gutenberg

1  Asked on January 1, 2021 by paranoid-android

   

How do I get the category URL from get_the_category?

2  Asked on December 28, 2020 by egr103

       

$wpdb -> Batch insert from XML File?

3  Asked on December 27, 2020 by click-here

         

lazy load comments wordpress on click

1  Asked on December 27, 2020 by sa-eu

   

Hook to use when customer placing order

1  Asked on December 26, 2020 by kevsterino

 

WordPress Ajax Custom Query – High CPU LOAD

0  Asked on December 22, 2020 by techn9ne

     

Update to older wordpress version from admin?

4  Asked on December 20, 2020 by hakre

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP