Skip Navigation

[Resolved] Min/max price search

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

Problem:

Setup some custom filters on custom fields, see details here:

https://toolset.com/forums/topic/min-max-price-search/#post-1228254

Solution:

It needs custom codes, see solution here:

https://toolset.com/forums/topic/min-max-price-search/#post-1232570

Relevant Documentation:

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

This support ticket is created 5 years, 8 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 13 replies, has 2 voices.

Last updated by Annie 5 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#1228254

I have a custom post type for rental properties, where I have separate minimum and maximum rent fields, since there's a range of prices for different apartments in the property. And we're trying to set up a search where users can type in a minimum and maximum price range they want to search.

Right now it's set to check if the maximum from their search is less than the maximum rent on the property, and it checks if the minimum rent is greater than the minimum price from their search. But that doesn't actually work right because it's not accounting for the other field, so if you have a property with a range of 800 - 1,200, and the user searches 700 - 1000, they won't find that property because it's only searching one field each. What it would need is to check both the min and max on the property for each of the min/max search fields. But there doesn't seem to be any way to search a single field more than once, after you add a filter that field doesn't show up in the list of fields to filter by anymore.

Is there some way to accomplish a search like I'm looking for?

#1228339

Hello,

I assume we are talking about this case:
A custom post type "property", with two custom numeric fields:
- minimum price
- maximum price

In your case, you just need to use field "minimum price" in Views filter, for example:
Select items with field:
minimum price is a number between URL_PARAM(wpv-wpcf-total-acreage_min), URL_PARAM(wpv-wpcf-total-acreage_max).

Then if user search 700 - 1000, the property with a range of 800 - 1,200 will be able to be outputted.

#1228831

If I do that on just the minimum price field like you listed, it would ignore any matches in the max price field. And if I have it do a "between" check on both the min and max fields it would exclude anything where it only found a match in one of them.

#1229098

Yes, in my opinion, you need only to check the minimum price, the max price field is not useful at all.

Please elaborate the question with more details:
it would ignore any matches in the max price field

Make some examples for it, thanks

#1229505

If you're only checking the minimum price field you will miss ones that match in the maximum price field. Example: a property with a rent range of $800 to $1,200, and your search is for properties with a min of $900 and a max of $1,200, you wouldn't find the example property because $800 in the field is too low to be a match. But it should match based on $1,200 in the max field.

#1229806
min-max-price.JPG

In your case, you can add setup two custom field filters:
Select items with field:
- minimum price is a number between URL_PARAM(wpv-wpcf-minimum-price_min), URL_PARAM(wpv-wpcf-minimum-price_max)

OR

- maximum price is a number lower than or equal URL_PARAM(wpv-wpcf-minimum-price_max)

See screenshot min-max-price.JPG

Then it should be able to cover both cases:
- searches 900 ~ 1200
- searches 700 ~ 1000

The post $800 to $1,200 will be able to outputted in the search result.

#1230205

The problem there is that I have other search parameters as well, the price range is just the one I was having trouble with, and if I change it from AND to OR it will do that for all of the search parameters. As far as I know there's not any way to limit the OR part to the price range, is there?

#1230773

Yes, you are right, There isn't such a built-in feature within Views plugin, in your case, you might consider custom codes, for example, use wpv_filter_query to trigger a PHP function:

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

In this PHP function, setup the AND to OR for custom field filters, see example of WordPress document:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

The 'meta_query' clauses can be nested in order to construct complex queries. For example, show products where color=orange OR color=red&size=small translates to the following:

#1231105

So for that would I need to remove all the filters from the View and then insert them all using the meta_query? Or do I use the filters and Views and just switch the AND/OR part using the meta_query somehow?

#1231263

wpv_filter_query is a filter hook, see the document I mentioned above:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

That means you can change the parameter of view's setting from AND to OR, you don't need to setup insert them all using the meta_query.
Since it is a custom PHP codes problem, if you need more assistance for it, please provide a test site with the same problem, point out the problem page URL and custom PHP codes URL, I can setup a demo for you.

#1231888

I'm not seeing how to use the hook to switch out the AND/OR for just that specific set of fields. I could provide a login to a development copy of the site if you want.

#1231942

OK, I have enabled the private message box again, please provide your website credentials and FTP access, also point out the problem page URL and custom PHP codes URL, thanks

#1232570

Thanks for the details, I have done below modifications in your website:
1) Edit the post view "Property Search Results":
hidden link

Change the max and min price filter as I mentioned above:
https://toolset.com/forums/topic/min-max-price-search/#post-1229806

2) Edit the custom PHP codes as below:
hidden link:

<?php

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

add_filter( 'wpv_filter_query', function($query_args, $view_settings, $view_id){
	if(in_array($view_id, array(3679))){
      	foreach($query_args['meta_query'] as $k => $meta_query){
        	if($meta_query['key'] == 'wpcf-price-min'){
              	$price_min_query = $meta_query;
              	$price_min_key = $k;
            }
        	if($meta_query['key'] == 'wpcf-price-max'){
              	$price_max_query = $meta_query;
              	$price_max_key = $k;
            }
        }
      	if(isset($price_min_key, $price_min_key)){
        	unset($query_args['meta_query'][$price_max_key]);
          	unset($query_args['meta_query'][$price_min_key]);
          	$query_args['meta_query'][] = array(
            	$price_min_query,
            	$price_max_query,
              	'relation' => 'OR',
            );
        }
        //var_dump($query_args['meta_query']);
	}
	return $query_args;
}, 99, 3 );

Please test again, check if it is what you want. thanks

#1232984

Looks to be working now. Thanks.