Ben Downey
Ben Downey

Reputation: 2665

How to get ordered results with WPQuery

I've got an events custom post type and I'm trying to order them but nothing I do seems to change their order. I'm using Advanced Custom Fields, so I'm setting the meta_key to the attribute I care about.

Does anything look off about this query?


function events_archive( $query ) {
  if( is_admin() ) {
    return $query;
  }

  if( $query->is_post_type_archive('events')  && $query->is_main_query()) {
    $query->set('meta_key', 'start_date');
    $query->set('orderby',  'meta_value');
    $query->set('order', 'ASC');
    $query->set('meta_query', array(
                                  array(
                                    'key' => 'start_date',
                                    'value' => date('Ymd'),
                                    'type' => 'DATE',
                                    'compare' => '>=',
                                    ) 
                                 )
    );
    return $query;
  }
}

I'm getting results, but I've noticed that I can swap $query->set('order', 'ASC') with $query->set('order', 'DESC'); and it doesn't change the order.

I've seen some similar SO posts that recommend using $query->set('orderby', 'meta_value_num'); instead of $query->set('orderby', 'meta_value'); but that doesn't seem to have any effect o the ordering.

Any suggestions on how to get ordering to work as expected?

Upvotes: 0

Views: 139

Answers (3)

DubVader
DubVader

Reputation: 1072

You might try using the meta value as a date as well as setting the meta type. When you do the actual meta query, you are specifying the date for which to query posts, but you are not telling the query to order them by the date necessarily.

function events_archive( $query ) {
  if( is_admin() ) {
    return $query;
  }

  if( $query->is_post_type_archive('events')  && $query->is_main_query()) {
    $query->set('meta_key', 'start_date');
    $query->set('meta_type', 'DATE');
    $query->set('orderby',  'meta_value_date');
    $query->set('order', 'ASC');
    $query->set('meta_query', array(
                                  array(
                                    'key' => 'start_date',
                                    'value' => date('Ymd'),
                                    'type' => 'DATE',
                                    'compare' => '>=',
                                    ) 
                                 )
    );
    return $query;
  }
}

Upvotes: 1

Johannes
Johannes

Reputation: 67748

Since it's a custom post type, you should use meta_key instead of keyin your array, like

'meta_key' => 'start_date',

and

'orderby' => 'meta_value_num',
'order' => 'DESC',

for the ordering (or ASC, whichever you need)

Upvotes: 0

I'm Joe Too
I'm Joe Too

Reputation: 5840

By default, ordering happens alphabetically but you're passing it a number (a formatted date as 'Ymd'). Try adding meta_value_num instead of meta_value in your orderby clause. Here's more info on that.

Upvotes: 0

Related Questions