admin管理员组文章数量:1122826
I'd like to associate new posts created under a custom post type to automatically be given a custom taxonomy term. As a heads up here are the parameters
Custom Post Type Name: business_ser_dir_ltg
Custom Taxonomy Name: secondary-listing-category
Term Name: Business Services
Term Slug: business-services
function bus_default_terms( $post_id, $post, $update ){
if ( 'business_ser_dir_ltg' == $post->post_type) {
$default_term = '5480'; // ID of the required term
$taxonomy = "business-services"; // slug of the taxonomy
wp_set_post_terms( $post_id, $default_term, $taxonomy );
}
}
add_action( 'save_post', 'bus_default_terms', 30, 3 );
This is the code I created, but when I create a new post it does not automatically assign the term Business Services to the newly created post.
I'd like to associate new posts created under a custom post type to automatically be given a custom taxonomy term. As a heads up here are the parameters
Custom Post Type Name: business_ser_dir_ltg
Custom Taxonomy Name: secondary-listing-category
Term Name: Business Services
Term Slug: business-services
function bus_default_terms( $post_id, $post, $update ){
if ( 'business_ser_dir_ltg' == $post->post_type) {
$default_term = '5480'; // ID of the required term
$taxonomy = "business-services"; // slug of the taxonomy
wp_set_post_terms( $post_id, $default_term, $taxonomy );
}
}
add_action( 'save_post', 'bus_default_terms', 30, 3 );
This is the code I created, but when I create a new post it does not automatically assign the term Business Services to the newly created post.
Share Improve this question asked Aug 6, 2024 at 21:40 Felicia SantosFelicia Santos 1012 bronze badges 1 |2 Answers
Reset to default 0First of all, the third param of wp_set_post_terms
must be a taxonomy slug, in this case, it's secondary-listing-category
.
Second, the term ID should be in numeric type instead of string. Read the explanation here: https://developer.wordpress.org/reference/functions/wp_set_post_terms/#user-contributed-notes
Therefore, the correct code should be
function bus_default_terms($post_id, $post, $update){
if('business_ser_dir_ltg' == $post->post_type){
$default_term = 5480; // ID of the required term
$taxonomy = "secondary-listing-category"; // slug of the taxonomy
wp_set_post_terms($post_id, intval($default_term), $taxonomy);
}
}
add_action('save_post', 'bus_default_terms', 30, 3);
Thank you @Dao for your answer. I was able to figure out the solution. The answer is below:
/*Set Default Taxonomy to Business Services*/
function bus_default_terms($post_id, $post, $update){
if('business_ser_dir_ltg' == $post->post_type){
$default_term = 'Business Services'; // Name of the required term
$taxonomy = "secondary-listing-category"; // slug of the taxonomy
wp_set_post_terms($post_id, $default_term, $taxonomy);
}
}
add_action('save_post', 'bus_default_terms', 30, 3);
本文标签: Assign Default Taxonomy to Post When Created
版权声明:本文标题:Assign Default Taxonomy to Post When Created 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736297862a1930102.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$taxonomy
, it's the slug of the taxonomy (secondary-listing-category
) and not the slug of the term. look the documentation here : developer.wordpress.org/reference/functions/wp_set_post_terms – mmm Commented Aug 6, 2024 at 22:40