由于没有直接的函数可以实现所需的功能,所以我编写了代码。
代码是不言自明的,您可以按自己喜欢的方式对其进行调整。
<?php
//Assuming a post ID to reset tags.
$postid = 172;
//Assuming tag2 is supposed to be removed
$remove_tag = \'tag2\';
//Collecting all the tags of post
$total_tags = get_the_tags($postid);
//Recreating an array without the $remove_tag
foreach($total_tags as $tag){
if($tag->name != $remove_tag){
$updated_tags[] = $tag->name;
}
}
//Setting tags with $updated_tags array.
wp_set_post_terms( $postid, $updated_tags, \'post_tag\', false);
?>
如果您正在处理多个帖子,则可以运行
foreach
回路和旁路
postid
在每个循环中。
EDIT更新代码以包含许多帖子(帖子id)
<?php
//Assuming a post ID to reset tags.
$posts_to_remove_tag_from = array(172,168);
//Assuming tag2 is supposed to be removed
$remove_tag = \'tag2\';
//Collecting all the tags of post
foreach($posts_to_remove_tag_from as $postid){
$total_tags = get_the_tags($postid);
//Recreating an array to without the $remove_tag
foreach($total_tags as $tag){
if($tag->name != $remove_tag){
$updated_tags[] = $tag->name;
}
}
//Setting tags with $updated_tags array.
wp_set_post_terms( $postid, $updated_tags, \'post_tag\', false);
//flushing $updated_tags array, and make it ready for next post in the loop.
$updated_tags = [];
}
?>