Mass delete unpopular Tags

坚强是说给别人听的谎言 提交于 2019-12-08 05:15:47

问题


Mass delete unpopular Tags

I have 1000's of tags and I would like to simply delete all tags that aren't used more than X times... i.e. 5 times.

Does anyone know of a simple way to do this? Even straight SQL would totally ROCK!


回答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);
    }
}

There's more efficient ways of doing it but this will do the job.

Edit: make a backup first. I'm not entirely sure that that table is exclusive to tags.




回答2:


Doing with with an SQL command is going to be your best bet. The tables wp_terms, wp_term_relationships, and wp_term_taxonomy are the ones of interest. WordPress keeps track of relationships with wp_term_taxonomy.count. So this help make life easier.

-- 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;

Note: I would strongly suggest making a backup of these tables before running the commands above.




回答3:


Jasons answer worked except in my install, the name of the taxonomy was 'post_tag' not 'tag'



来源:https://stackoverflow.com/questions/4539954/mass-delete-unpopular-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!