问题
I have a problem with my code, it seems that my order/orderby is not working on my WP Query, when i set the order value to DESC, its not changing still ASC when i check on wpdb lastquery.
Here's my code:
$args = array(
'post_status' => 'publish',
'posts_per_page' => $per_page,
'paged' => $paged,
'orderby'=> 'date',
'order'=> $sortBy,
'posts_per_page' => $per_page,
'post_type'=> 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $catby,
'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'.
'suppress_filters' => false
)
),
'date_query' => array(
array(
'after' => $rangeby
)
)
);
But when i use this simple args DESC order is working:
$args = array(
'post_status' => 'publish',
'posts_per_page' => $per_page,
'paged' => $paged,
'orderby'=> 'date',
'order'=> 'desc',
'suppress_filters' => true,
'post_type'=> 'product'
);
Can someone help me about this? Thanks!
回答1:
I would start by echoing the value of $sortBy, in the example that works you're not using that variable, so it probably not returning the value 'date'.
来源:https://stackoverflow.com/questions/59206460/wp-query-on-order-and-orderby-is-not-working