Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. function delete_tags_with_no_posts(){
  2. $tags = get_tags( array('number' => 0,'hide_empty' => false));
  3. foreach ( $tags as $tag ) {
  4. $tag_id = $tag->term_id;
  5. $args = array( 'post_type' => 'post', 'posts_per_page' => -1,
  6. 'tax_query' => array(
  7. array(
  8. 'taxonomy' => 'post_tag',
  9. 'field' => 'id',
  10. 'terms' => $tag_id
  11. )
  12. )
  13. );
  14. $postslist = get_posts( $args );
  15. $count_posts_with_this_tag = count($postslist);
  16. if ($count_posts_with_this_tag < 1 ){
  17. wp_delete_term( $tag_id, 'post_tag' );
  18. }
  19. }
  20. }
  21. add_action( 'wp_head', 'delete_tags_with_no_posts' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement