Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function delete_tags_with_no_posts(){
- $tags = get_tags( array('number' => 0,'hide_empty' => false));
- foreach ( $tags as $tag ) {
- $tag_id = $tag->term_id;
- $args = array( 'post_type' => 'post', 'posts_per_page' => -1,
- 'tax_query' => array(
- array(
- 'taxonomy' => 'post_tag',
- 'field' => 'id',
- 'terms' => $tag_id
- )
- )
- );
- $postslist = get_posts( $args );
- $count_posts_with_this_tag = count($postslist);
- if ($count_posts_with_this_tag < 1 ){
- wp_delete_term( $tag_id, 'post_tag' );
- }
- }
- }
- add_action( 'wp_head', 'delete_tags_with_no_posts' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement