1

I'm attempting to display a list of all posts from multiple categories (if anyone answers this, you might want to include combining tags as well, as probably that will make this answer comprehensive for future seekers).

Here's what I'm working with:

This function works to show a list:

function my_custom_loop_three_posts($category, $tag, $offset) {
        $args=array(
          // showposts has been replaced, use 'posts_per_page' instead
          // 'showposts'        => 1,
          'posts_per_page'      => 3,
          // this has been replaced, use 'ignore_sticky_posts'
          // 'caller_get_posts' => 1,
          'ignore_sticky_posts' => true,
        );

        if ($category) {
          $args['cat'] = $category;
        }

        if ($tag) {
          $args['tag'] = $tag;
        }

        if ($offset) {
          $args['offset'] = $offset;
        }

        $my_query = new WP_Query($args);
        // ... rest of function to output loop 

if( $my_query->have_posts() ) {
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
             <div class="">
              <a href="<?php the_permalink() ?>" >
   <div class="menu-item-list-item menu-item-list-item-not-title">
      <?php the_title(); ?>
   </div>
              </a>
             </div>
            <?php
            endwhile;
        } //if ($my_query)

        wp_reset_query();  // Restore global post data stomped by the_post().   
 };


function myFilter($query) {
    if ($query->is_feed) {
        $query->set('cat','-121');
    }
return $query;
}
add_filter('pre_get_posts','myFilter');

And I just use this to display the list on a page:

<?php my_custom_loop_three_posts(330, NULL, 1); ?>

Because this function works fine, I was hoping to be able to modify it to show multiple categories / tags in the results, but am a bit stumped on this. Maybe it's easy, maybe hard? Solutions OR pointing me towards how to fix this welcome. Thanks

2
  • When you say combine, do you just mean display posts from multiple categories? Commented Jul 29, 2016 at 10:05
  • Yes, correct. So instead of a list of posts from cat-33, the list would show most recent posts from cat-33 plus cat-44 plus cat-55, or if you were going with tags: animals, plants and planets
    – Jay
    Commented Jul 29, 2016 at 10:12

1 Answer 1

0

This type of behavior is documented in the WP_Query documentation. You can use a comma-separated list of category IDs instead of just one. For example (ripped right off the Codex page):

$query = new WP_Query( array( 'cat' => '2,6,17,38' ) );

With tags it's a little different, but not too much so:

$query = new WP_Query( array( 'tag__in' => array( 37, 47 ) ) );

You might be able to use it like the cats with comma-separated ID's, but I'm not sure off the top of my head without testing it and the docs are a little unclear on that point. But basically, with a few tweaks, if necessary, you could be using the same function and call it like:

<?php my_custom_loop_three_posts('33,44,55', array(12,13,14), 1); ?>

If using that example, just make sure you change

if ($tag) {
    $args['tag'] = $tag;
}

to:

if ($tag) {
    $args['tag__in'] = $tag;
}

Hopefully this makes sense. Docs should get you the rest of the way there.

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.