Friday, April 1, 2011

How to Mass Delete Wordpress Tags that has 0 Count

As WordPress Query: [updated as per #]

$wpdb->query("
DELETE a,c
FROM
$wpdb->terms AS a
LEFT JOIN $wpdb->term_taxonomy AS c ON a.term_id = c.term_id
LEFT JOIN $wpdb->term_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id
WHERE (
c.taxonomy = 'post_tag' AND
c.count = 0
)
");
?>

##############################################################

Mysql Query (for use in PHPMYADMIN):

DELETE a,c
FROM
database.prefix_terms AS a
LEFT JOIN database.prefix_term_taxonomy AS c ON a.term_id = c.term_id
LEFT JOIN database.prefix_term_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id
WHERE (
c.taxonomy = 'post_tag' AND
c.count = 0
)



Adjusting "database" and "prefix_" to your WordPress database name and WordPress table prefix (assuming they're not just default).
I've tested in both WordPress and PHPMYADMIN, using MySQL 5.1 and PHP5.
Maybe a little bit late, but it may be of help to someone... :)