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?

1 Answer 1


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.

  • 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. Apr 11, 2012 at 9:10
  • 1
    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. Apr 11, 2012 at 9:15
  • 1
    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, 2012 at 9:18
  • @Offroff - yup, but a better way would be to use the meta_query parameter :). Apr 11, 2012 at 9:22
  • One more question … can I do the same sorting for the archives page? I added 'has_archive' => 'events' to my custom-post-template. /events displays now my "archive.php" file where I already do this for my headline elseif ( 'wr_event' == get_post_type() ) : ?> - my post-template is called "wr_event". I have two probably little questions with this. 1.) How can I also sort the posts on the archive page with the mate-key? Of course just for my events? And how can I only display events that are "over". Right now all events show up in the archive … Apr 11, 2012 at 14:42

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.