大量删除不受欢迎的标签
我有1000个标签,我只想删除所有使用不超过X次的标签,即5次。
有人知道这样做的简单方法吗?甚至直接的SQL也将完全失败!
$x = 5; // set this to any number
$sql = "SELECT `name` FROM `wp_terms`";
$result = mysql_query($sql);
$count = array();
while($row = mysql_fetch_assoc($result))
{
$count[$name]++;
}
foreach($count as $key = $value)
{
if($value < $x)
{
$sql2 = "DELETE FROM `wp_terms` WHERE `name` = '". $key ."'";
$result2 = mysql_query($sql2);
}
}
有更有效的方法可以做到这一点。
编辑:首先进行备份。我不完全确定该表是标签专用的。
使用SQL命令是最好的选择。表wp_terms
,wp_term_relationships
和wp_term_taxonomy
是感兴趣的表。 WordPress使用wp_term_taxonomy.count
跟踪关系。因此,这有助于使生活更轻松。
-- remove terms
DELETE FROM wp_terms WHERE term_id IN (SELECT term_id FROM wp_term_taxonomy WHERE taxonomy = 'tag' AND count <= 5;)
-- remove all relationships
DELETE FROM wp_term_relationships WHERE term_taxonomy_id IN (SELECT term_taxonomy_id FROM wp_term_taxonomy WHERE taxonomy = 'tag' AND count <= 5;)
-- remove taxonomy entry
DELETE FROM wp_term_taxonomy WHERE taxonomy = 'tag' AND count <= 5;
注:我强烈建议在运行上述命令之前对这些表进行备份。
Jasons答案有效,但在我的装置中,分类法的名称是'post_tag'而不是'tag'
请确保首先备份数据库
DELETE a,c FROM wp_terms AS a
LEFT JOIN wp_term_taxonomy AS c ON a.term_id = c.term_id
LEFT JOIN wp_term_relationships AS b on b.term_taxonomy_id = c.term_taxonomy_id
WHERE ( c.taxonomy = 'post_tag' AND c.count <= 5 );
在最后一行,您还可以修改c.count <= 5
并用您选择的任意数字替换5。