Skip Navigation

[Resolved] Min/Max price search filter of custom price field

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by Minesh 6 months, 3 weeks ago.

Assisted by: Minesh.

Author
Posts
#2704348
01.PNG

Hi,
I need to make two input search fields - min price and max price, that sort content independently.
More information - i need input field 1 to be price from and input field 2 to be price above and when I search field 1 to show only results from that price.

The "price" field is another plugin custom field and it's single - no min and max values, just numbers.
Sorting is working fine, but I cannot make it work with the search parameters.

This is the shortcode Minesh from support added to my custom search:
[wpv-control-postmeta field="price" type="input" default_label="Цена" url_param="wpv-price"]
This works fine if I write singe price, but the website I'm developing for a client will have hundreds of listings and I need to make it more user friendly.

I tried conditional block, but the block does not recognize the shortcode from above.

#2704587

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To filter with price range, you can use the following filter shortcode that will add Price From and To textboxes where user can input minimum and maximum price:

<div class="form-group">
	<label for="wpv-price_min">[wpml-string context="wpv-views"]Price From[/wpml-string]</label>
	[wpv-control-postmeta field="price" type="textfield" placeholder="From" url_param="wpv-price_min"]
</div>
<div class="form-group">
	<label for="wpv-price_max">[wpml-string context="wpv-views"]Price To[/wpml-string]</label>
	[wpv-control-postmeta field="price" type="textfield" placeholder="To" url_param="wpv-price_max"]
</div>

And you can setup the query filter as given under:

Select items with field:
price is a number between URL_PARAM(wpv-price_min), URL_PARAM(wpv-price_max)

Please check the following screenshot:
- hidden link

#2704627
004.PNG
003.PNG
002.PNG
001.PNG

Hi, thanks once again for the fast reply.

I added the query filter to my search view and the shortcode to the search bar, but it's not working.

#2704653

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

It looks like what you want is:

- If user input both min and max price then you want to display result that falls between the provided price min and max range.
- if user input only min range then you want to display result that falls greater than provided min price.
- if user input only max range then you want to display result that falls less than the provided max price.

Is that correct? If not, please share your exact requirement how it should work.

#2704662

Hi, sorry - English is not my native language.

But yes, what i meant by "work independently" in my first comment is exactly what you described.

When user places only price greater than X, get results for that. When user places both price greater and lower than X, get results that fall between those numbers.

#2704806

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok can you tell me how I can see view on frontend? where you added exactly?

#2704977

The view is located on this page - hidden link, it's called "SEARCH LIST".

#2704984

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've added the following filter hook to "Custom code" section offered by Toolset:
- hidden link

add_filter('wpv_filter_query', 'func_adjust_price_range', 10, 3);
function func_adjust_price_range($query_args, $setting,$view_id) {
   
    if($view_id == 5846) {
       
       foreach((array)$query_args['meta_query'] as $k=>$v):
        		 if(isset($v['key']) and $v['key']=='price'){
         			unset($query_args['meta_query'][$k]);
                 }
        endforeach;
      
      $compare = $value = '';
      if(isset($_GET['wpv-price_min']) and isset($_GET['wpv-price_max'])){
      
        
        if(!empty($_GET['wpv-price_min']) and !empty($_GET['wpv-price_max']) and                               is_numeric($_GET['wpv-price_min']) and is_numeric($_GET['wpv-price_max'])){
            
          	$compare = "BETWEEN";
            $value = $_GET['wpv-price_min'].",".$_GET['wpv-price_max'];
          
        }else if(!empty($_GET['wpv-price_min']) and is_numeric($_GET['wpv-price_min']) and empty($_GET['wpv-price_max'])){
          	  	$compare = ">";
                $value = $_GET['wpv-price_min'];
        }else if(!empty($_GET['wpv-price_max']) and is_numeric($_GET['wpv-price_max']) and empty($_GET['wpv-price_min'])){
          	  	$compare = "<";
                $value = $_GET['wpv-price_max'];
        }
              if($compare!='' and $value!='') {

                 $query_args['meta_query'][] = array('key'=>'price',
                                              'value'=>$value,
                                              'type'=>'NUMERIC',
                                              'compare'=> $compare);
              }
       }
      }
    return $query_args;
}

Can you please confirm it works as expected now.

#2705012

Thank you!
I just edited the code to be higher/smaller or equal.