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];