Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is it possible to display a wordpress category page but have it pull posts from mulitple categories?

For example, let's say I need to pass in params for cat_id in this fashion

/category/dining/?location=2&activity=2

And have the resulting page produce a list of posts from the categories specified by location and activity.

Thanks!

share|improve this question
Please tell me, did my solution helped you? – petermolnar Apr 5 '11 at 18:24

1 Answer

Yes, it is.

You'll need a template page for the category (for example, category-30.php, where 30 is the id of the category you want to show with multiple subcategories).

In the template, run through the standard loop for post from the original category if you want to, if not, the magic function will be wp_reset_query and query_posts.

The first is needed for not to have a headache, the second needs some parameters to show the posts you need from the certain categories.

If you want to do this nice and easy to adjust, create an array, with the needed params

$cats = array('location','activity');

Run through the array, and create an own loop inside: ( original example from query_posts function reference)

foreach ($cats as $getparam ) :
  $query = 'cat=' . $_GET[$getparam] . '&orderby=date&order=ASC'; // concatenate the query
  query_posts( $query ); // run the query

  if (have_posts()) : 
    while (have_posts()) : 
      the_post();
    [here comes your part]
    endwhile; 
  endif;
endforeach;

That's all.

EDIT: thinking it through, you'd be better with a really simple line at the top if the category-xx.php template:

query_posts( 'cat=' . $_GET['location'] . ',' . $_GET['activity'] . '&orderby=date&order=ASC' );

This creates all posts from two categories, defined by $_GET params, also resetting original results for the category. The upper solution displays results separately, and you question was to have all posts from multiple categories, sorry.

share|improve this answer

Your Answer

 
discard

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