Skip Navigation

[Resolved] Assign default category to a custom post type

This thread is resolved. Here is a description of the problem and solution.

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 3 years, 10 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 4 replies, has 2 voices.

Last updated by mashaB 3 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#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

Languages: English (English )

Timezone: 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

Languages: English (English )

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