Site icon Hip-Hop Website Design and Development

Unable to get all tags from specific categories

Page in question.

I’m trying to use a WP_Query(); targeting specific categories and displaying all the tags used for posts within those categories.

I noticed that some tags, like truffles aren’t being included in the results even though that post is categorized as a recipe and it’s other category, sweets, is also included in the category array.

Any thoughts on what I’m doing wrong so as to get only some of the tags? Thanks!

    <ul class="tag-list">
    <?php

      $query = new WP_Query( 'cat=4,101,94,93,56,72,99,100,63,98,95,96,80' );

        $posttags = "";
        if (have_posts()) : while (have_posts()) : the_post();

            if( get_the_tag_list() ){
                $posttags = $posttags . get_the_tag_list('',',',',');
            }

        endwhile; endif; 

      wp_reset_postdata();  

        // Explode tags in array
        $sortedtags = explode(',', $posttags);

        // Sort array
        asort($sortedtags);            

        // Remove duplicates from array
        $sortedtags = array_unique($sortedtags);

        // Remove the blank entry due to get_the_tag_list
        $sortedtags = array_values( array_filter($sortedtags) );

        foreach ($sortedtags as $tagname) {
        echo '<li>' . $tagname . '</li>';
        }

    ?>
    </ul>

Update

I thought my used of the main loop if (have_posts)...the_post(); was screwing things up so I edited the code, but I’m still missing my my mushrooms! 😛

    <ul class="tag-list">
    <?php

  $query_args = array( 'cat' => '4,101,94,93,56,72,99,100,63,98,95,96,80' );
  $query = new WP_Query( $query_args );

    $posttags = "";
    while( $query->have_posts() ) {
        $query->the_post();
    if( get_the_tag_list() ){
        $posttags = $posttags . get_the_tag_list('',',',',');
    }
    }

  wp_reset_postdata();  

    // Explode tags in array
    $sortedtags = explode(',', $posttags);

    // Sort array
    asort($sortedtags);            

    // Remove duplicates from array
    $sortedtags = array_unique($sortedtags);

    // Remove the blank entry due to get_the_tag_list
    $sortedtags = array_values( array_filter($sortedtags) );

    foreach ($sortedtags as $tagname) {
    echo '<li>' . $tagname . '</li>';
    }

    ?>
    </ul>