批量删除不受欢迎的标签

时间:2010-12-27 16:04:36

标签: wordpress tags sql-delete

批量删除不受欢迎的标签

我有1000个标签,我想简单地删除所有未使用超过X次的标签...即5次。

有谁知道这样做的简单方法?即使是直接的SQL也完全是ROCK!

4 个答案:

答案 0 :(得分:2)

使用SQL命令将是您最好的选择。表格wp_termswp_term_relationshipswp_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;

注意:我强烈建议您在运行上述命令之前备份这些表。

答案 1 :(得分:1)

请确保先备份数据库

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替换为您选择的任意数字。

答案 2 :(得分:0)

Jasons回答工作除了在我的安装中,分类法的名称是'post_tag'而不是'tag'

答案 3 :(得分:-1)

$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);
    }
}

有更有效的方法可以做到这一点,但这可以胜任。

编辑:先备份。我不完全确定该表是标签所独有的。