wordpress order posts by meta_value date in DESC order
wordpress order posts by meta_value date in DESC order
I am trying to sort a query to display posts in order of a meta_key value. The value is stored like so: 2018-Mar-23
.
2018-Mar-23
I would like to display posts with a date that is after today, with the closest date for example 2018-Aug-30
being shown first, and the furthest date away 2020-Aug-30
to show last.
2018-Aug-30
2020-Aug-30
I have put a query together, which still mixes all dates up. Here is what i have:
$args = array(
'posts_per_page' => -1,
'post_type' => 'games' ,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_parent' => 0,
'meta_query' => array(
array(
'key' => 'releasedate',
'value' => date("Y-M-d"),
'type' => 'DATE',
'compare' => 'BETWEEN',
)
)
);
For some reason i can only get this query to work using the compare method "BETWEEN"
. Using ">"
which i would expect to use, returns no results.
"BETWEEN"
">"
Any ideas?
Thanks
'orderby' => 'meta_value',
compare' => '>',
1 Answer
1
Can you try this
$args = array(
'posts_per_page' => -1,
'post_type' => 'games' ,
'orderby' => 'meta_value',
'order' => 'ASC',
'post_parent' => 0,
'meta_key' => 'releasedate',
'meta_query' => array(
array(
'key' => 'releasedate',
'value' => date("Y-M-d"),
'type' => 'DATE',
'compare' => '>',
)
)
);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you try
'orderby' => 'meta_value',
andcompare' => '>',
– Sudharshan Nair
Aug 29 at 13:05