只需在中添加缺少的布尔参数wp_set_object_terms()
功能如下:
wp_set_object_terms($post->ID, $term->term_id, \'product_cat\', true);
这次学期将是
appended 且未更换。
在添加该术语之前,应检查该术语在产品上是否不存在,如:
add_action( \'transition_post_status\', \'new_product_add\', 10, 3 );
function new_product_add( $new_status, $old_status, $post ) {
if(
$old_status !== \'publish\'
&& $new_status === \'pending\'
&& ! empty($post->ID)
&& $post->post_type === \'product\'
) {
$taxonomy = \'product_cat\';
$term_id = get_term_by( \'name\', \'parent_category\', $taxonomy )->term_id;
if( ! has_term( $term_id, $taxonomy ) ) {
wp_set_object_terms( $post->ID, $term_id, $taxonomy );
}
}
}