Mass delete unpopular Tags
Mass delete u开发者_如何学编程npopular 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!
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.
Make sure to back up the database first
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 );
On the last line, you could also modify c.count <= 5
and replace 5 with any number of your choice.
Jasons answer worked except in my install, the name of the taxonomy was 'post_tag' not 'tag'
$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.
精彩评论