Sort custom-posts in archive.php via meta-key?

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x