So, I'm trying to get the maximum price of all 'properties' (custom post type).

$max_price_query = new WP_query(array(
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'post_type' => 'properties',
    'nopaging' => true,
    'orderby' => 'meta_value_num',
    'meta_key' => '_price',
    'order' => 'DESC',

if ($max_price_query->have_posts()) {
    while ($max_price_query->have_posts()) : $max_price_query->the_post();
        $pid = $post->ID;
        $price_max = get_post_meta($pid, '_price', true);
        echo '<pre>' . $pid . '</pre>';

First problem is that they are ordered by ID, not price. The properties are called Property 1, Property 2, etc., So in this case, ordered by ID == ordered by Title == ordered by Date.

Second problem, is that, before being ordered by ID, they are actually ordered by another meta_value called _featured_prop (which is either 0 or 1). Well this is because, I also have the following:

 function set_query_parameters($query) {
    if( !is_admin() &&  is_main_query() && is_post_type_archive( 'properties' ) ) {
        $query->set('meta_key', '_featured_prop');
        return $query;
      add_action( 'pre_get_posts', 'set_query_parameters' );

I don't understand why this pre_get_posts seems to be affecting my new WP_Query since I have specified 'is_main_query()' in the if statement. I thought it wouldn't affect my secondary_query.

When echoing the ID's I'm essentially getting: 26, 24, 20, 27, 25, 23, 22, 21, 19. Where 26, 24, and 20, have _featured_prop = 1.

If it makes any difference, the first code snippet is being called in a sidebar.php file which is being called into archive-properties.php. Elsewhere, in a functions file I am calling in function in the second code snippet (since you can't do an add_action of pre_get_posts after pre_get_posts is fired, before loading the archive page).

Can anybody give me some help?

  • you need to check the current query with $query->is_main_query()
    – Milo
    Commented May 6, 2016 at 22:55
  • You cannot order by meta value if you don't include meta query with relation = first. You cannot order by something that doesn't exist. This has been the case in my previous experiences. Secondly, I don't quite follow this part: So in this case, ordered by ID == ordered by Title == ordered by Date. Are you really ordering by all of those things? You must have very beefy hardware, ordering by some many things (especially meta values) is extremely expensive.
    – N00b
    Commented May 6, 2016 at 23:04
  • I'm not actually sorting by all three, it just so happens that sorting by either of those three would return the same thing. Ie. if you make the posts one after another, their ID's will be in order. And if you name them post 1, post 2, post 3, etc. then sorting by title returns the same as sorting by date, which returns the same as sorting by ID. These aren't real posts, just some examples for testing purposes.
    – Joel M
    Commented May 7, 2016 at 18:08

1 Answer 1


As @Milo suggested and also codex documented it is_main_query Under_the_Hood

This function is an alias for the method WP_Query::is_main_query(). In filter or action hook callbacks that are passed the WP_Query object, such as pre_get_posts, it is circular to call this function. Instead, directly call the passed object's method. For example, if your filter callback assigns the passed WP_Query object to $query, you would call the method like so: $query->is_main_query()

You must change if conduction to

function set_query_parameters($query) {
    if( !is_admin() &&  $query->is_main_query() && is_post_type_archive( 'properties' ) ) {
        $query->set('meta_key', '_featured_prop');
add_action( 'pre_get_posts', 'set_query_parameters' );

There is no need to return the query. And I really wonder why you are not receiving a NOTICE because WordPress also trigger _doing_it_wrong() on using is_main_query() in this way!

  • 1
    Thanks for the help, it seems to have fixed my problems. Price is also sorting properly. I had to temporarily set to WP_Debug to false because it's interfering with another plugin, and I haven't gotten around to fixing it. It did indeed give me a warning about using is_main_query().
    – Joel M
    Commented May 7, 2016 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.