Advanced Search by minimum/maximum values

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.

Read More

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.

Related posts

Leave a Reply

1 comment