WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom-taxonomy for a custom-post-type just like categories.

I've used the standard loop in my taxonomy-event_type.php template so far …

if ( have_posts() ) : while ( have_posts() ) : the_post(); 

This meant that the template only showed the posts associated with the current term (category) the post was in. So on url/event_type/workshops only all posts that were set to "workshops" in the backend would appear.

Now i've changed that standard loop to a custom loop where I'm passing some args so I can sort the posts by a meta-value (the event date)

$args = array(
    "post_type" => "wr_event",
    "meta_key" => "_wr_event_date",
    "orderby" => "meta_value_num",
    "order" => "ASC"
    //"terms" => 

    $wr_events = new WP_Query( $args );

    if ( $wr_events->have_posts() ) : while ( $wr_events->have_posts() ) : $wr_events->the_post();

This works perfect. Only thing. It breaks the standard behaviour for the term (category) I'm currently viewing.

So with this custom loop when viewing url/event_type/workshops or url/event_type/lectures always the same posts appear! How can I again view only the posts associated with the current term "workshops" or "lectures"?

Thank you for your help!


if I do …

$current_cat = get_query_var( 'event_type' );
echo $current_cat;

$args = array( …

The $current_cat holds the right term (category) I'm currently in. Only problem is that the posts in the loop are always the same. How can I filter by this term?

share|improve this question
up vote 3 down vote accepted

You second query performs an entirely new query and does not have the terms set.

Besides, it's not as efficient to 'redo' the query. Instead, hook into pre_get_posts and change the order there:

function change_order_for_events( $query ) {
    //Check if currenty query is the 'main query' and event type taxonomy being viewed 
    if ( $query->is_main_query() && is_tax('event_type')) {
        $query->set( 'meta_key', '_wr_event_date' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'ASC' );
add_action( 'pre_get_posts', 'change_order_for_events' );

For bonus points you might want to check that the 'orderby' and 'order' paramters are not set - that way ordering by date becomes default for events on the event type taxonomy pages but can be overridden if required.

All conditionals are available to you.

share|improve this answer
Wow, thank you that looks promising, but doesn't work for me! I've gone back to the standard loop in my taxonomy_event_type.php template and inserted your function into my functions.php … however the posts in my taxonomy template appear in theier post_date order and not in their event-order. Did I miss anything? Of course the default behaviour with just showing the right posts associated with its "category" would be back that way - exactly what I want. Just the sorting doesn't take affect. – mathiregister Apr 11 '12 at 9:10
Should have been $query->is_main_query() (3.3+ only). You should put WP into debug mode when developing - that should have flagged up the error. – Stephen Harris Apr 11 '12 at 9:15
Great answer. If you only want to show future events you can also add $yesterday = time() - 24*60*60; $query->set('meta_value', $yesterday); $query->set('meta_compare', '>'); (this will show future events and events in the last 24 hours) – offroff Apr 11 '12 at 9:18
Thank you very much! – mathiregister Apr 11 '12 at 9:19
@offroff Thank you too, just what I needed :) – mathiregister Apr 11 '12 at 9:21

Your Answer


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

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