Skip Navigation

[Resolved] Custom taxonomy default category

This support ticket is created 3 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 9 replies, has 2 voices.

Last updated by helenaJ-2 3 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#2080589

Hello,
I have created a new taxonomy for the posts "books" and I would like that if we do not select any by default it appears or is linked to the category "literary". Sometimes we forget to add this taxonomy and it will make it easier for us to automatically add it to posts that we have not selected any category within books.

How can I do this?

#2080851

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Helena,

Thank you for getting in touch.

Is this for the backend or frontend? Meaning are the posts created through the wordpress backend or are they done using the Frontend Forms from our Toolset forms plugin.

Please let me know.
Thanks,
Shane

#2081293
Captura de Pantalla 2021-06-09 a les 10.30.26.png

Hello,

With Worpress. When I create a new post (article).
I need the custom taxonomy created with Toolset to have a default category if we don't select any.

Attached image.
Thanks

#2081605

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Helena,

You can use the function below to do this.


function set_default_category($post_id) {
   
    if ( !has_term('', 'genre')  && get_post_type($post_id) == 'post_type' ) {
 
        wp_set_post_terms( $post_id, 'term', 'genre');
    }
}
add_action( 'save_post', 'set_default_category' );

Add it to your Toolset custom code section in Toolset -> Settings -> Custom Code and then activate it.

Replace 'post_type' with the slug of your Post Type, 'genre' with the slug of your custom taxonomy and 'term' with the slug of the taxonomy term that you want to assign.

Please let me know if this helps.
Thanks,
Shane

#2082673
Captura de Pantalla 2021-06-10 a les 11.11.27.png
Captura de Pantalla 2021-06-10 a les 11.20.39.png
Captura de Pantalla 2021-06-10 a les 11.05.02.png

Hi Shane,

unfortunately it doesn't work for me. 🙁

I am attaching some screenshots so you can see if there are any errors.

Thanks

#2083475

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Helena,

I did some checks on the wp_set_post_terms() function and it only works for the default category taxonomy.

To get this to work for a custom taxonomy switch the function to.
wp_set_object_terms()

However your completed code should be

function set_default_category($post_id) {
    
    if ( ! has_term('', 'genre')  && get_post_type($post_id) == 'post' ) {
  
        wp_set_object_terms( $post_id, 'funk', 'genre');
    }
}
add_action( 'save_post', 'set_default_category' );

Ofcourse substituting your values.

Thanks,
Shane

#2083477

Thank you!

#2083495

Perfect!

Now I want to do the same but with the product categories and I see that the function does not work for me. You can help me?

 function set_default_category($post_id) {
    
    if ( !has_term('', 'sense-categoria')  && get_post_type($post_id) == 'product' ) {
  
        wp_set_post_terms( $post_id, 'product_cat', 'sense-categoria');
    }
}
add_action( 'save_post', 'set_default_category' ); 
#2083531

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Helena,

Try using this below.

function set_default_product_category($post_id) {
     
    if ( !has_term('', 'product_cat')  && get_post_type($post_id) == 'product' ) {
   
        wp_set_object_terms( $post_id, 'sense-categoria', 'product_cat');
    }
}
add_action( 'save_post', 'set_default_product_category' );

Thanks,
Shane

#2083541

My issue is resolved now. Thank you!