Navigation überspringen

[Gelöst] Assign default category to a custom post type

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 vor 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 -

Zeitzone des Unterstützers: America/Jamaica (GMT-05:00)

Dieses Thema enthält 4 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von mashaB vor 4 years, 11 months.

Assistiert von: Shane.

Author
Artikel
#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
Unterstützer

Sprachen: Englisch (English )

Zeitzone: 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
Unterstützer

Sprachen: Englisch (English )

Zeitzone: 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.