2

So I have a wp meta query that is handling the searching of events which have a start timestamp, and an end timestamp, I want to return all results within the two dates a user picks in the search. This part is working fine as of now.

However, I want to sort the results by the start timestamp, so the most recent events show up first.

$args = array(
    's' => $_GET['k'],
    'post_type' => 'event',
    'posts_per_page' => '-1',
    'post_status' => array( 'private','publish' ),

    'meta_key' =>'_start_ts',
    'orderby' => 'meta_value_num',
    'order' => 'asc',

    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => '_end_ts',
            'value' => array(strtotime($_GET['d1']),strtotime($_GET['d2'])),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
        ),
        array(
            'key' => '_start_ts',
            'value' => array(strtotime($_GET['d1']),strtotime($_GET['d2'])),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
        )
    )
);

If I seach for all events in the month of april, we get the following timestamp ordering:

1397865600, 1398211200, 1398384000, 1397520000, 1398297600

how do I get wordpress to sort by _start_ts?

1 Answer 1

0

Maybe I'm looking too simply, but your code sets the order_by for the query here:

'orderby' => 'meta_value_num',

If you change that to "post_date" it should put them in date order, though you may want to look at the other sorting options here:

WordPress get_posts function

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.