简体   繁体   中英

Bug in wp_query? Wordpress

$query_args = array(
'post_type' => 'post',
'posts_per_page' => 6,
'order'=> 'desc',
'orderby' => 'meta_value date',
'paged' => $paged
);

This is my current query to sort posts. What it does that it shows posts with any post meta first and then sorts the remaining ones by date. However in the WordPress Reference it says that meta_key should be present for it to work.

What's actually going on here?

To answer your question on "what is actually going on here?":

To order posts by a meta key you need the following in your query:

'meta_key' => 'custom_date',
'orderby' => 'meta_value',
'order' => 'DESC',

Along with your other query params.

meta_key is the name of the field that you wish to sort by (wherever you have stored the date that you want to sort by)

orderby tells WP that you want to use the value stored in the selected meta_key. You can also tell wp what type of data to expect eg 'meta_value_num'

order is the direction you want to order the posts.

'orderby' => 'meta_value date' is not a valid parameter.

You could have:

'orderby' => 'date' which would order by the post date, or a meta key, but wordpress doesnt have the ability to say "this 'meta_key' is in 'date' format. You have to do that yourself.

https://wordpress.stackexchange.com/questions/177070/filter-query-based-on-date-in-custom-field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM