Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

<?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.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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>';
  }

} 
share|improve this answer
    
Sorry, I should have mentioned that the code is being run on pages, so it doesn't have any categories assigned to it. I'd also like to avoid that because I don't want to have these list pages coming up in category pages. –  Valency Jul 30 '12 at 6:31
    
no prob. i've updated my answer for that. –  peteroak Jul 30 '12 at 7:08
    
I was getting some syntax errors (and still am) trying to get this code to run. It wouldn't execute at all, so i had to change array ( 'tag' -> $posttag ) to array( 'tag' => $posttag ) and am now getting a bunch of these: Warning: strpos() expects parameter 1 to be string, object given in /home/beatex/public_html/wp-includes/query.php on line 1787 Warning: preg_split() expects parameter 2 to be string, object given in /home/beatex/public_html/wp-includes/query.php on line 1788 Any ideas? Thanks for your help so far! –  Valency Jul 31 '12 at 1:24
    
you're right! sorry bout that. the relevant change is 'tag' => $posttag->name –  peteroak Jul 31 '12 at 2:32
    
Getting there... Errors have disappeared, but now it's just printing 'Read posts about' over and over. It's printed it ~130 times, which is weird because there's only 90 tags total on the whole site. Although maybe that's getting all tags from all the posts in that category, which means there would be dupes. You can take a look here if you need: beatexplorers.com/testpage –  Valency Jul 31 '12 at 5:29
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.