Hi,
I’m really sorry if this thread has been doubled. I got an error by submitting the first one, and my message was totally cleared.
I tried to order my custom post-type (called “vehicle”) by a custom date field I created, called “date_sell”. I tried different ways (with “meta_value” and “meta_value_num” as “orderby” value), but none of them has worked. Here’s the simpliest code I tried to find the problem.
Simple function :
function eai_get_sold_vehicles(){
$args = array(
'posts_per_page' => -1,
'post_type' => 'vehicle',
'order' => 'DESC',
'meta_key' => 'date_sell',
'orderby' => 'meta_value_num' // also tried with 'meta_value'
);
$query = new WP_Query($args);
if($query->have_posts()) : while ($query->have_posts() ) : $query->the_post();
echo '<br />' . get_field("date_sell");
endwhile;
endif;
}
Return :
23/12/2018
24/10/2018
07/10/2018
09/12/2018
12/11/2018
Here the vehicles are listed as they come in the post-type list, so with the default behaviour, ordered by date of publication.
I’ve searched on other threads, but I still can’t find what I did wrong here.
Can someone please help me on this ?
Thank you in advance,
N.
I forgot to reply my own post. I find the issue and the resolution. I post it if anyone ever has the same problem.
Actually, I was using a re-order plugin on the post-type I tried to query, called Post-Types-Order.
This plugin seems to automaticaly apply a filter for all queries that are pointed the re-ordered post-type. Even in your own code.
There’s a way to prevent that in the API, by adding a parameter in the query args :
ignore_custom_sort' => true
You can find an example here : https://www.nsp-code.com/advanced-post-types-order-api/sample-usage/ (Don’t need to get advanced version to use it).
I found the problem and resolved it.
I was using a plugin which re-order the post-type I tried to call.
This plugin is called Reorder-Post-Type.
It seems to apply a filter on all queries that concerns a post-type affected by the plugin, even in your own code.
If you want to prevent that, there’s a parameter that you can add in your query args, called ignore_custom_sort => true, like this :
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'order' => 'DESC',
'ignore_custom_sort' => true,
'meta_key' => 'your_custom_field',
'orderby' => 'meta_value'
);