Skip Navigation

[Resolved] custom search filter price range dropdown

This thread is resolved. Here is a description of the problem and solution.

Problem:
custom search filter price range dropdown

Solution:
To implement price range filter with view you need to use view's filter hook "wpv_filter_query".

You can find the proposed solution, in this case with the following reply:
https://toolset.com/forums/topic/custom-search-filter-price-range-dropdown/#post-1249349

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 5 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Our next available supporter will start replying to tickets in about 2.20 hours from now. Thank you for your understanding.

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 4 replies, has 2 voices.

Last updated by chong-sumw 5 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1249245
Screen Shot 2019-05-24 at 7.28.29 PM.png

Hi there, I need something supposed to be trivial and common yet I don't know how to achieve with toolset view. What I need to do is providing a dropdown menu for user to select price range.

Precisely, when user want to filter e.g. $1000-$2000 via the custom search form, a dropdown menu showing a few options, namely,

1. $0-1000
2. $1000-$2000
3. $2000-$3000

If the user select $1000-$2000 and click search, it would filter all post/product fitting that range. I have written the html but don't know how to feed the selected option into views/wordpress.

Thank you for your help, please feel free to log in and check it out.

View id: 536 (Business Archive View)
page: hidden link

#1249349

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - To do such thing, I've adjust your query filters and removed the unwanted content from "Search and Pagination" section, as well as I, 've also adjusted the query filters from view's "Query Filer" section. As you can see with your view here:
=> hidden link

Then, I've added the following code to Toolset's Custom Code section:
=> hidden link


add_filter('wpv_filter_query', 'search_between_numeric_func', 10, 3);
function search_between_numeric_func($query_args, $setting,$view_id) {
  
    if($view_id == 536) {
      
     
        foreach((array)$query_args['meta_query'] as $k=>$v):
      
      		unset($query_args['meta_query'][$k]);
            if(isset($v['key']) and $v['key']=='wpcf-selling-price'){
              
                    $query_args['meta_query'][$k]['key'] = 'wpcf-selling-price';
                	$query_args['meta_query'][$k]['type'] = 'NUMERIC';
                    $query_args['meta_query'][$k]['compare'] = 'BETWEEN';
                    $query_args['meta_query'][$k]['value'] = str_replace("-",",",$v['value']);
                        
              
            }else if(isset($v['key']) and $v['key']=='wpcf-store-size-sqft'){
                  
                        $query_args['meta_query'][$k]['key'] = 'wpcf-store-size-sqft';
                        $query_args['meta_query'][$k]['type'] = 'NUMERIC';
                        $query_args['meta_query'][$k]['compare'] = 'BETWEEN';
                        $query_args['meta_query'][$k]['value'] = str_replace("-",",",$v['value']);
              
            }else if(isset($v['key']) and $v['key']=='wpcf-profit'){
                  	
                        $query_args['meta_query'][$k]['key'] = 'wpcf-profit';
                        $query_args['meta_query'][$k]['type'] = 'NUMERIC';
                        $query_args['meta_query'][$k]['compare'] = 'BETWEEN';
                        $query_args['meta_query'][$k]['value'] = str_replace("-",",",$v['value']);
            }
            
           	
        endforeach;
      $query_args['meta_query']['relation'] = 'AND';
    
    }
    return $query_args;
}

And now, I can see the filters are working as expected.
=> hidden link

#1249391
Screen Shot 2019-05-24 at 9.33.50 PM.png

Thanks Minesh it was very helpful.

However, now it pops up a debug window, screen cap as shown in attachment. Not sure if i press anything wrong or what now the site is not working.

I want to add a default "Please choose" option as the first option in the dropdown menu, Moreover, a for example, "$4m+" as the last option in the dropdown. It would be much appreciated if you could tell me which part of the code should I change in order to accomplish that.

#1249403

Minesh
Supporter

Languages: English (English )

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

However, now it pops up a debug window, screen cap as shown in attachment. Not sure if i press anything wrong or what now the site is not working.
=> I disabled the debug popup.

To add "Please choose" you should adjust the other fields shortocde as given under where we added 0 with values and "Please chose" with display_values attributes respectively within search and pagination section. You can follow the same for last option.

[wpv-control-postmeta field="wpcf-selling-price" type="select" source="custom" url_param="wpv-wpcf-selling-price" values="0,0-250000,250000-500000,500000-1000000,1000000-3000000" display_values="Please choose,below $250k,$250k-500k,$500k-1m,$1m-3m"]
#1249453

Very helpful My issue is resolved now. Thank you Minesh!