您可以使用wp_insert_comment 检测何时向帖子添加新评论的操作。然后在您的操作回调中,您可以使用get_comments_number(), 并使用wp_set_object_terms().
沿着这些路线,
add_filter(
\'wp_insert_comment\',
function($id, $comment) {
if ( (int) $comment->comment_approved ) {
$post_id = (int) $comment->comment_post_ID;
$post_comment_count = get_comments_number(
$post_id
);
if ( $post_comment_count > 3 ) {
wp_set_object_terms(
$post_id,
array(\'3-comments\'),
\'category\',
false // don\'t append, overwrite terms
);
} else if ( $post_comment_count === 2 ) {
wp_set_object_terms(
$post_id,
array(\'2-comments\'),
\'category\',
false // don\'t append, overwrite terms
);
} else {
wp_set_object_terms(
$post_id,
array(\'1-comment\'),
\'category\',
false // don\'t append, overwrite terms
);
}
}
},
10,
2
);