【发布时间】:2010-07-28 18:58:58
【问题描述】:
我有一个按帖子 ID 添加标签的功能,但现在我想在添加新标签之前删除该 ID 的所有当前标签?
有人知道执行此操作的内置函数或 wordpress 查询吗?
-哈德森
【问题讨论】:
我有一个按帖子 ID 添加标签的功能,但现在我想在添加新标签之前删除该 ID 的所有当前标签?
有人知道执行此操作的内置函数或 wordpress 查询吗?
-哈德森
【问题讨论】:
如果可以直接访问数据库(PhpMyAdmin),最快的方法是 SQL 请求。
查看法典中的database schema:
DELETE FROM wp_term_relationships
WHERE wp_term_relationships.object_id = 'yourPostId'
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.taxonomy = 'post_tag';
警告:显然,直接修改数据库是危险的。这样做要小心。
【讨论】:
【讨论】:
这是我拼凑起来的。它的纯数据库工作,因为我相信没有快速的功能可以通过 ID 删除标签,(但我仍然不是 100% 确定)。
//define array of tags
$tags = array("new tag","new tag 2");
//define post ID
$val = 254;
//delete tags associated to post id
$query = "SELECT * FROM ".$table_prefix."term_relationships tr JOIN ".$table_prefix."term_taxonomy tt ON tr.term_taxonomy_id=tt.term_taxonomy_id WHERE tr.object_id='$val' AND tt.taxonomy='post_tag'";
$result = mysql_query($query);
if (!$result){echo $query; echo mysql_error();}
while ($arr = mysql_fetch_array($result))
{
$tid = $arr['term_taxonomy_id'];
$query2 = "DELETE FROM ".$table_prefix."term_relationships WHERE term_taxonomy_id='$tid'";
$result2 = mysql_query($query2);
if (!$result2){echo $query2; echo mysql_error();}
}
//add tags to post id
wp_add_post_tags($val,$tags);
【讨论】: