Skip Navigation

[Resolved] Assign default taxonomy term to custom post type automatically

This support ticket is created 5 years, 6 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1239538

Hi

I have this custom post type ( Cities ) which has been created with toolset. Now I'd like to automatically assign the default 'new' taxonomy term to this post type every time a new post is created ( hidden link ).

I have used this code but it doesn't work
/*
* Set default taxonomy term on custom post
*/
function tssupp_default_term( $post_id, $post, $update ){

// don't mess with existing posts
if ( !$update ) {

$default_term = '3'; // ID of the required term
$taxonomy = "new"; // slug of the taxonomy
wp_set_post_terms( $post_id, $default_term, $taxonomy );

}
}
add_action( 'save_post_city', 'tssupp_default_term', 101, 3 );

Let me know pls if you need access credentials to my site and the FTP account

Sincerely

#1239540

Hi, the code looks pretty good at first glance. Is "new" a flat taxonomy or a hierarchical taxonomy? Flat taxonomy terms should be set using names, not IDs, according to the wp_set_post_terms documentation: https://codex.wordpress.org/Function_Reference/wp_set_post_terms

#1239541

Oh and I just noticed that you're using the save_post_city hook. That hook isn't supported for post types registered by Toolset, and should not be used. An example showing how to apply a conditional per post type is available here:
https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

#1239549

It's a hierrarchical taxonomy ( hidden link )
I used this code now but i throws an error

function tssupp_default_terms( $post_id, $post, $update ){
if ( 'city' == $post->post_type) {

$default_term = '3'; // ID of the required term
$taxonomy = "new"; // slug of the taxonomy

wp_set_post_terms( $post_id, $default_term, $taxonomy );

}
}
add_action( 'save_post', 'tssupp_default_terms', 30, 2 );

#1239557

Try updating the number of params to 3 instead of 2:

add_action( 'save_post', 'tssupp_default_terms', 30, 3 );

That's because there are 3 parameters here:

function tssupp_default_terms( $post_id, $post, $update ){
#1239574

I'm not getting the error anymore but nothings happens

function tssupp_default_terms( $post_id, $post, $update ){
if ( 'city' == $post->post_type) {

$default_term = '3'; // ID of the required term
$taxonomy = "new"; // slug of the taxonomy

wp_set_post_terms( $post_id, $default_term, $taxonomy );

}
}
add_action( 'save_post', 'tssupp_default_terms', 30, 3 );

Don't I need to insert the "if ( !$update ) {" somewhere?

Regards

#1239575

Don't I need to insert the "if ( !$update ) {" somewhere?
If you only want this code to be applied to new posts, yes. But if nothing is happening with the code now, adding the conditional won't fix anything. May I log in to see how this is set up?

#1239580
Screen Shot 2019-05-05 at 3.52.45 PM.png

The term slug is "new", but that's not what we're looking for in this code. The taxonomy slug is "city-type". See the attachment here. Please update $taxonomy to be "city-type" in your code and try again.

#1239588

Awesome Help 🙂 My issue is resolved now. Thank you!