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.

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

2 Answers 2

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
    
query_posts is considered harmful –  Weston Ruter Aug 16 '13 at 3:04
    
Could you please describe this a bit more, maybe even pointing to a link? Coming up with a less "harmful" solution would also be a very good point. –  petermolnar Aug 29 '13 at 9:32
    
    
Oh, ok. In this particular case, the one above, it's a replacement for the main loop. If it was a secondary loop, I would agree. –  petermolnar Sep 2 '13 at 8:12
    
If it's replacing the main loop, then it should modify the request in the pre_get_posts action. –  Weston Ruter Sep 2 '13 at 16:18

Yes, given a requested URL like:

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

In your functions.php (or in a plugin) add:

function add_location_activity_query_vars( $query_vars ) {
    $query_vars[] = 'location';
    $query_vars[] = 'activity';
    return $query_vars;
}
add_filter( 'query_vars', 'add_location_activity_query_vars' );

function location_activity_categories_query( &$query ) {
    if ( $query->get( 'category_name' ) !== 'dining' ) {
        return;
    }
    $category = get_category_by_slug( $query->get( 'category_name' ) );
    if ( empty( $category ) ) {
        return;
    }
    $cats = array( $category->term_id );
    if ( is_numeric( $query->get( 'location' ) ) {
        $cats[] = $query->get( 'location' );
    }
    if ( is_numeric( $query->get( 'activity' ) ) {
        $cats[] = $query->get( 'activity' );
    }
    $query->set( 'category__and', $cats );
}
add_action( 'pre_get_posts', 'location_activity_categories_query' );

And that should do it. I think you actually want to do category__and and not cat, since you want the query params to filter down the results.

share|improve this answer

Your Answer

 
discard

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