Passer la navigation

[Résolu] Assign default category to a custom post type

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:

The problem here is that the user wanted to assign a default category to their post when the post is submitted.

Solution:
You can assign a default taxonomy to a post using the hook below.

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

Add the function to your Toolset custom codes in Toolset -> Settings -> Custom Code and activate it.

You will need to replace 'post' with the slug of your custom post type, 'funk' with the taxonomy term slug and 'genre' with the taxonomy slug.

Once you've done this then whenever your users create a post with no taxonomy assigned then one will automatically get assigned.

This support ticket is created Il y a 4 years, 11 months. 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 -

Fuseau horaire du supporter : America/Jamaica (GMT-05:00)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par mashaB Il y a 4 years, 11 months.

Assisté par: Shane.

Auteur
Publications
#2109507

Tell us what you are trying to do?
I want my custom post type have default category assigned to them when an editor adds a new post of that type

Thanks!

#2109531

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Masha,

You can assign a default taxonomy to a post using the hook below.

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

Add the function to your Toolset custom codes in Toolset -> Settings -> Custom Code and activate it.

You will need to replace 'post' with the slug of your custom post type, 'funk' with the taxonomy term slug and 'genre' with the taxonomy slug.

Once you've done this then whenever your users create a post with no taxonomy assigned then one will automatically get assigned.

Thanks,
Shane

#2109559

The post says that it is waiting to the user confirmation, but I do not see any answer from the supporter here.

#2109565

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Masha,

Not sure what happened but let me repost my response.

You can assign a default taxonomy to a post using the hook below.

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

Add the function to your Toolset custom codes in Toolset -> Settings -> Custom Code and activate it.

You will need to replace 'post' with the slug of your custom post type, 'funk' with the taxonomy term slug and 'genre' with the taxonomy slug.

Once you've done this then whenever your users create a post with no taxonomy assigned then one will automatically get assigned.

Thanks,
Shane

#2109755

Thank you, Shane! With slight modifications I was able to achieve what I need based on your recommendation.