I have a custom post type where there is an option to sort by date. There is however a custom date field that allows changing the display date. This custom date field is not always filled.
Right now I'm trying to sort by date, and if the custom date field is present use that field when sorting instead.
I have the following that doesn't really work since I believe orderby sorts by the custom field and then tacks on sorted entry dates.
$args = array(
'post_type' => array('post'),
'order' => 'DESC',
'orderby' => array('upcoming_class_date' => 'DESC', 'date' => 'DESC'),
'category_name' => 'upcoming class',
'meta_key' => 'company',
'meta_value' => $company,
'showposts' => 40
);
$loop = new WP_Query( $args );
I was thinking of grabbing the results and resorting them after query, but that seems very inefficient.
I have a custom post type where there is an option to sort by date. There is however a custom date field that allows changing the display date. This custom date field is not always filled.
Right now I'm trying to sort by date, and if the custom date field is present use that field when sorting instead.
I have the following that doesn't really work since I believe orderby sorts by the custom field and then tacks on sorted entry dates.
$args = array(
'post_type' => array('post'),
'order' => 'DESC',
'orderby' => array('upcoming_class_date' => 'DESC', 'date' => 'DESC'),
'category_name' => 'upcoming class',
'meta_key' => 'company',
'meta_value' => $company,
'showposts' => 40
);
$loop = new WP_Query( $args );
I was thinking of grabbing the results and resorting them after query, but that seems very inefficient.
Share Improve this question edited Apr 22, 2019 at 19:19 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Apr 22, 2019 at 18:38 ChrisChris 1255 bronze badges1 Answer
Reset to default 1Few issues I see here with your code:
You have
category_name
set toupcoming class
... category slugs do not have spaces in them, so this is incorrect. If you meant to have allupcoming
andclass
categories, then useupcoming,class
https://developer.wordpress/reference/classes/wp_query/#category-parametersYou have a meta query on
company
but it sounds like you're also trying to sort by a custom fieldupcoming_class_date
, and as such, this field also needs to be included in your query. You will need to change this to an advanced meta query to handle this. https://developer.wordpress/reference/classes/wp_query/#custom-field-post-meta-parametersYou said a custom post type -- but your code has
post
as the post type
This is some quick example code I came up with on how you should setup your query arguments:
$args = array(
'post_type' => array( 'post' ),
'order' => 'DESC',
'orderby' => array( 'upcoming_class_date' => 'DESC', 'date' => 'DESC' ),
'category_name' => 'upcoming class',
'showposts' => 40,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'company',
'value' => $company,
'compare' => '=',
),
array(
'relation' => 'OR',
array(
'key' => 'upcoming_class_date',
'compare' => 'EXISTS',
),
array(
'key' => 'upcoming_class_date',
'compare' => 'NOT EXISTS',
)
)
),
);
You will notice there is an EXISTS
and NOT EXISTS
.. this is because you said that sometimes a post will not have a value for that field -- to make sure the value is included in the query, we have to add both of those and set the relation
to OR
(that way it returns posts with that field and ones without).
The other potential issue I see is the format the date is saved in -- which could cause sorting problems, but you did not provide details on the format.
I recommend reading this answer on a similar question to better understand how WP_Query works with meta queries: https://wordpress.stackexchange/a/285012/51201
You can also do an orderby
using the meta query array key (but they have to be changed to associative array) -- there are examples in the link above for that.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745564950a4633337.html
评论列表(0条)