Home Forums Bug Reports Meta_Query Not Working Properly?


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(
                 '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

    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.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.