0

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! :P

    <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>
4
  • Realizing that when I'm on the second page, only that page's displayed posts are showing in the tag column. Thought that the new WP_Query() would search all posts related to that category array.
    – tigre
    Commented Nov 19, 2014 at 0:44
  • WP_Query does exactly what you tell it to do, nothing more, it won't second guess you with things like pagination etc. Are you trying to replace what WordPress grabs in the main query with your own stuff? If so then the pre_get_posts filter is what you want. Also, hardcoding category numbers is very bad practice, perhaps you should use a custom taxonomy to define what goes where and use term names rather than hardcoded category term IDs
    – Tom J Nowell
    Commented Nov 19, 2014 at 1:44
  • Hey @TomJNowell, thanks for the tip. Will swap out those cat IDs for slugs. The structure of the categories is Recipes (parent) and, e.g. Salads (child). I read that category_in might target the parent and all its children, which would be ideal.
    – tigre
    Commented Nov 19, 2014 at 1:50
  • Also, I'm trying to display all tags within specified categories. I thought WP_Query would be able to filter that with args limiting to specific categories.
    – tigre
    Commented Nov 19, 2014 at 1:57

2 Answers 2

0

Looks like all I needed was to include 'posts_per_page' => -1 in the $query_args array.

Final, working code:

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

        $query_args = array(
            // Easier to call the parent category, "recipe" instead of dealing with all these ids
            // 'cat' => '101,94,93,56,72,99,100,63,98,95,96,80,4',
            // Calling recipes (4) and ingredients (56) category ids - specific recipe types that are children of recipes get included
            'category__in' => array( 4, 56 ),
            // Grabs all posts, not just the ones on the first page
            'posts_per_page' => -1
        );
        $query = new WP_Query( $query_args );
        //  $query = new WP_Query( 'author_name=centehua' );

        $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>
1
  • What if I want to use this as a template_part and want args to identify the category to which the user accessed the page? Because in this way it is necessary to identify the category "manually". Something with: $ categories = get_the_category (); 'category__in' => $ categories [0] -> slug, How could I do it? Commented Nov 29, 2020 at 21:09
0

you're trying to concatenate the tag lists for all posts into a single string, which may not be the best approach and issue with how you are retrieving and concatenating the tags within your loop.

<ul class="tag-list">
    <?php
    $query = new WP_Query('cat=4,101,94,93,56,72,99,100,63,98,95,96,80');

    $all_tags = array();

    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
            $post_tags = get_the_tags();

            if ($post_tags) {
                foreach ($post_tags as $tag) {
                    $all_tags[] = $tag->name;
                }
            }

    endwhile;
    endif;

    wp_reset_postdata();

    // Sort and remove duplicates
    $sorted_tags = array_unique($all_tags);
    asort($sorted_tags);

    foreach ($sorted_tags as $tag_name) {
        echo '<li>' . $tag_name . '</li>';
    }
    ?>
</ul>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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