Mass delete unpopular Tags

别来无恙 提交于 2019-12-08 17:34:27
$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.

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.

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

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