this question is strongly related to this question where I already got a working answer.

I have a custom-post-template named “wr_event” and a custom-taxonomy named “event_type”.

This “event-posts” have a meta-box where I set the event date and I use this …

function change_order_for_events( $query ) {
    //only show future events and events in the last 24hours
    $yesterday = time() - 24*60*60; 

    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' );
        $query->set( 'meta_value', $yesterday );
        $query->set( 'meta_compare', '>' );
    }

}

add_action( 'pre_get_posts', 'change_order_for_events' );

… to sort all “event-posts” via its event-date.

This works just fine and does exactly what I want.

Only thing that doesn’t work so far is the “archive.php” where I want the same to take effect.

I added 'has_archive' => 'events' to my custom-post-template. /events displays now my “archive.php” file where I already use is_post_type_archive('wr_event') for my headline.

However I want my archived events also to be in its right date-order. So the same function above change_order-for_events() should work on my “archive.php” template. Of course in this case the function should show just old events that have already taken place.

Any idea how I could do that?

update:
I’m storing the timestamp with the MetaBox and CustomFields Class

$meta_boxes[] = array(
    'id'         => 'event_date',
    'title'      => 'Event Date',
    'pages'      => array( 'wr_event', ),
    'context'    => 'normal',
    'priority'   => 'high',
    'fields'     => array(
        array(
            'name' => 'Event Date Picker',
            'desc' => 'The date the event takes place',
            'id'   => $prefix . 'event_date',
            'type' => 'text_date_timestamp',
        )
    ),
);

I query it in my template files with

$wr_event_fields = get_post_custom();
$wr_event_fields['_wr_event_date'][0];

2 Answers
2

Just use the appropriate conditionals:

function change_order_for_events( $query ) {
    //only show future events and events in the last 24hours
    $yesterday = current_time('timestamp') - 24*60*60; 

    if ( $query->is_main_query() && (is_tax('event_type') || is_post_type_archive('wr_event')) ) {
        $query->set( 'meta_key', '_wr_event_date' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'ASC' );

        //Get events after 24 hours ago
        $query->set( 'meta_value', $yesterday );
        $query->set( 'meta_compare', '>' );

       //Get events before now
       //$query->set( 'meta_value', current_time('timestamp') );
       //$query->set( 'meta_compare', '<' );
    }

}

add_action( 'pre_get_posts', 'change_order_for_events' );

This will alter the main query for term archives for the taxonomy event_type and archives for the post type wr_event.

Leave a Reply

Your email address will not be published. Required fields are marked *