I’m guessing it’s because you used order_by
when it should actually be orderby
– that order_by
wasn’t just a typo in the question, right?
And also, if the meta value is a date, then you should also set the meta_type
parameter to DATE
so that the sorting works as expected.
So try with:
$the_query = new WP_Query( array(
'post_type' => 'event',
'posts_per_page' => 2,
'orderby' => 'meta_value', // it's orderby; not order_by
'meta_key' => 'start_date',
'meta_type' => 'DATE', // and set meta_type to DATE
) );
Update
If the above still doesn’t work, try using meta_query
instead of those meta_key
and meta_type
, and set the orderby
to start_date
– the key in the meta_query
for the metadata start_date
:
$the_query = new WP_Query( array(
'post_type' => 'event',
'posts_per_page' => 2,
'meta_query' => array(
'start_date' => array(
'key' => 'start_date',
'type' => 'DATE',
),
),
'orderby' => 'start_date',
) );
However, if there’s a plugin or custom code overriding the orderby
, e.g. via pre_get_posts
, then I doubt the above would work.
But you can try it, who knows it magically works… also, try clearing your site caches – just in case.