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.

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];
share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
Thank you, just perfect. Last question I have is how I can only display events which have already taken place - with their event-date in my archive? So the archive should sort them just like it does with your updated code but only show events that are over … any ideas on that too? –  mathiregister Apr 11 '12 at 16:29
    
How are you storing the end timestamp? Its simply a matter of altering the meta_compare and meta_value parameters... –  Stephen Harris Apr 11 '12 at 17:01
    
I updated my question with the sample code. I store the timestamp as unix timestamp with a custom class. This class just extends the standard metabox stuff … –  mathiregister Apr 11 '12 at 17:15
    
See updated answer - just use the meta_compare and meta_value as appropriate (commented out is how to get events before today). –  Stephen Harris Apr 11 '12 at 18:34
    
thank you, perfect! –  mathiregister Apr 11 '12 at 19:40
add comment

I'd use the meta_query parameter in the WP_Query heres the link

and try something like this:

$args = array(
'orderby' => 'meta_value_num',
    'order' => 'ASC',
'meta_query' => array(      
    array(
        'key' => '_wr_event_date',
        'value' => $yesterday,
        'compare' => '>'
    )
)
 );
$query = new WP_Query( $args );
share|improve this answer
    
Normally I'd use meta_query too. But this doesn't work unfortunately: you have to set meta_key when ordering by meta_value/meta_value_num even if a key is set in meta_query (presumably because of ambiguity that arises when you have multiple arrays in meta_query). –  Stephen Harris Apr 11 '12 at 16:08
add comment

Your Answer

 
discard

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.