1

I'm building an advanced search form which allows me to search by different parameters. Currently, searching by taxonomies work fine. My issue occurs with trying to set a minimum price to search by. Here's the code in my functions.php file:

function advanced_search_query($query) {
    if ($query->is_search) {
        $query->set('post_type', array( 'properties' ));

        if (isset($_GET['propertyfor'])) {
            $query->set('taxonomy', 'propertyfor');
            $query->set('terms', $_GET['propertyfor']);
        }

        if (isset($_GET['propertytype'])) {
            $query->set('taxonomy', 'propertytype');
            $query->set('terms', $_GET['propertytype']);
        }

        if (isset($_GET['minPrice'])) {
            $query->set('meta_query', array(
                'key' => 'shru_price',
                'value' => $_GET['minPrice'],
                'compare' => '>=',
                'type' => 'NUMERIC'
            ));
        }
    };
    return $query;
};
add_filter('pre_get_posts', 'advanced_search_query', 1000);

As you can see, I'm trying to use a meta query to handle it but nothing is happening. The minimum price is completely ignored when displaying results.

Anyone know how to fix?

Edit: FYI, the numeric values are stored in the database without any special characters.

UPDATE: It works for the following, where i set a min and max value and use BETWEEN:

    if (isset($_GET['minPrice']) && isset($_GET['maxPrice'])) {
        $query->set('meta_query', array(
            array(
                'key' => 'shru_price',
                'value' => array($_GET['minPrice'], $_GET['maxPrice']),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
            )
        ));
    }

but not for what I originally posted where only one is set and i use >= or <=. I used elseif statements between each but still no results were being produced.

1 Answer 1

5

A meta_query is an array of arrays. You only have an array.

    $query->set('meta_query', array(
        'key' => 'shru_price',
        'value' => $_GET['minPrice'],
        'compare' => '>=',
        'type' => 'NUMERIC'
    ));

It should be:

$query->set(
  'meta_query', 
  array(
    array(
      'key' => 'shru_price',
      'value' => $_GET['minPrice'],
      'compare' => '>=',
      'type' => 'NUMERIC'
    )
  )
);

And please validate/sanitize that user supplied data before using it !

6
  • you beat me by 12 secs :) ...
    – gmazzap
    Apr 7, 2014 at 13:48
  • @G.M. : Interesting that we both reformatted the code in (almost) the same way :)
    – s_ha_dum
    Apr 7, 2014 at 13:50
  • you certainly do earn your reputation ^_^
    – CoreyRS
    Apr 7, 2014 at 13:56
  • I updated the post with an interesting issue. Any idea of the cause behind it?
    – CoreyRS
    Apr 7, 2014 at 14:07
  • The SQL come out correct when I test >=. I don't know what to say about that secondary issue.
    – s_ha_dum
    Apr 7, 2014 at 14:11

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.