Home › Forums › Bug Reports › Meta_Query Not Working Properly?
Im trying to do a query where i get certain posts ordered by 2 ACF fields, they are both numeric, but its not returning the proper order, its like their treating it like text, like 2 is above 1 and also above 12. Did i miss something here?
$args = array (
'posts_per_page' => 10,
'post_type' => 'my_post_type',
'post_status' => $post_status,
'meta_query' => array(
array(
'relation' => 'AND',
'year' => array(
'key' => 'year',
'compare' => 'EXISTS',
'type' => 'NUMERIC'
),
'num' => array(
'key' => 'num',
'compare' => "EXISTS",
'type' => 'NUMERIC'
)
)
),
'orderby' => array(
'year' => 'DESC',
'num' => 'DESC'
),
);
This is only a guess, but what I think you are running into is a conflict with an existing WP_Query parameter. “year” is an argument that already has a special meaning to WP_Query. Are the posts being ordered by the post date “Year”? Try changing your clause name to something else. It is generally a good idea to use things like “year_clause” and “num_clause” to make sure you don’t end up with conflicts in naming.
I’ve changed the parameter “year” to its translated noun in my language which is “ano”, in fact ive changed both of them to “ano” and “numero” to make a diference but still the same.
Still guessing here.
It could be that the fact that you are using 'compare' => 'EXISTS'
that 'type' => 'NUMERIC'
is being ignored. But I really do not know.
Honestly I would expect your query to work. However, take a look at this https://stackoverflow.com/questions/43624283/orderby-and-order-not-working-for-numeric-meta-query
Note that the numeric values that this person wants to order by uses nested meta queries using both ‘>=’ and ‘<=’ comparison with no values so that it will return anything that has any value except an empty string or is non-existing.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.