• I am trying to add a function that will filter search results, entered in an advanced search form, by multiple custom field values (year and price).

    I’m trying to get the URL filtering like below. The URL outputs fine from the search form, but not the filtering itself.

    I can get it to work for one at a time, but not both at the same time. For instance, the code below works to filter the price. If I replace all of the “price” fields to “year” it will also work. However, if I duplicate the codes (one for price, the other for year) only year seems to work.

    http://domain.com/?s=test&price_low=0&price_high=59&year_low=1900&year_high=2020

    //Price Filtering
    
    function wpa_filter_home_query( $query ){
        if( $query->is_search()
        && $query->is_main_query()
        && isset( $_GET['price_low'] )
        && isset( $_GET['price_high'] ) ) {
            $meta_query = array(
                array(
                    'key' => 'price',
                    'value' => array( $_GET['price_low'], $_GET['price_high'] ),
                    'type' => 'numeric',
                    'compare' => 'BETWEEN'
                )
            );
            $query->set( 'meta_query', $meta_query );
        }
    }
    add_action( 'pre_get_posts', 'wpa_filter_home_query' );

  • The topic ‘Filter search results by multiple value ranges (price/year)’ is closed to new replies.