Display tags that only appear in one category

I’m trying to get this less manual than it currently is. I want to display tags that appear in only a single category, so if they are in a post that has two categories, I want that tag ignored.

The following code works, but is a little clunky. Ideally I’d like to be able to skip having to add the excludes and just have it figure it out itself.

Read More
<?php 
function pl8_artist_list($catname, $exclude) {
    $custom_query = new WP_Query('posts_per_page=-1&category_name='.$catname.'&cat='.$exclude.'');
    if ($custom_query->have_posts()) :
        while ($custom_query->have_posts()) : $custom_query->the_post();
            $posttags = get_the_tags();
            if ($posttags) {
                foreach($posttags as $tag) {
                    echo '<h2>' . $tag->name . '</h2>';
                    echo '<p>' . $tag->description . '</p>';
                    echo '<p><a href="http://beatexplorers.com/artist/'. $tag->slug . '">Read posts about ' . $tag->name . '</a></p>';
                }
            }
        endwhile;
    wp_reset_postdata(); // reset the query
    endif;
} 
?>

And I call it like so:

<?php
pl8_artist_list(trance, -3,-11,-4,-8);
?>

The excludes being the other categories that the tags may appear in.

I’m quite new to PHP, so any light that could be shed would be greatly appreciated.

Related posts

Leave a Reply

1 comment

  1. This loop starts by fetching all posts in given category. then goes through each tag of current post, fetches the posts for each tag, and if all of the posts have 0 or 1 category, prints that category. So, if you have lots of tags, lots of posts, this could be slow-ish.

    function wa_60126_pl8_artist_list($catname) {
    
      //this part fetches the posts in the category provided
      $custom_query = new WP_Query('posts_per_page=-1&category_name='.$catname );
      if ($custom_query->have_posts()) :
          while ($custom_query->have_posts()) : $custom_query->the_post();
    
    
      $posttags = get_the_tags();
    
      // build the tag list
      if ($posttags) {
          foreach($posttags as $posttag) {
    
              // fetch the posts with same tags
              $posts_same_tag = get_posts( array ( 'tag' => $posttag->name ) );
              $is_safetag = TRUE;
              // check if has only one category
              foreach( $posts_same_tag as $st_post ) {
              // if it has more than one category, this tag is no good
                  if ( count ( wp_get_post_categories($st_post->ID)) > 1 ){
                      $is_safetag = FALSE; break;
                  }
              }
              // end of posts and its dirty?
              if($is_safetag) $safetags[] = $posttag;
          }
      }
      endwhile;
      wp_reset_postdata(); // reset the query
      endif;
      // now lets echo the safetags
      foreach( $safetags as $tag) {
            echo '<h2>' . $tag->name . '</h2>';
            echo '<p>' . $tag->description . '</p>';
            echo '<p><a href="http://beatexplorers.com/artist/'. $tag->slug . '">Read posts about ' . $tag->name . '</a></p>';
      }
    
    }