Skip Navigation

[Resolved] Number field for price on properties – Views filter not sorting properly

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

Problem:
How to allow comma to view's number field filter

Solution:
You can use view's filter hook wpv_filter_query to modify the view's query on fly and adjust it as per your requirement by hooking custom query filters to it.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/number-field-for-price-on-properties-views-filter-not-sorting-properly/#post-612098

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

This support ticket is created 6 years, 2 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
- 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)

Author
Posts
#611507
pmedc-price.jpg
pmedc-price-02.jpg

Hello,

I am creating a property listing search, and am having trouble with the price field.

First, I set the types field to number. When I do this, it seems I can't put currency formatting directly into the field.

I tried multiple formats (as shown in screenshots attached), and nothing seems to sort correctly.

Additionally, the custom search filter only returns results when there are no special characters in the field (10000, not $10,000).

Please let me know how I can get the property prices to sort properly, and to be formatted as currency.

Thanks so much!

#611575
pmedc-acreage.jpg

Since we are talking about numbers here, it seems that this is a bigger issue.

I have another field for Acreage, and the numbers are not sorting properly here either.

Also, when I select an option and submit the search, the property doesn't come up in the results. I think this is because of the decimal points.

When a number has decimals and/or commas, it should still sort properly, and the results should contain the correct properties.

Please advise.

Thanks!

#611620

Hello,

I decided to use a free form search for the price range (see attached).

I went through every property, and removed the commas and decimals.

When I search the numbers like "100000 - 750000" it shows correct results.

However, people aren't going to know that they can't use commas, and when I tried "100,000 - 750,000" the search has no results.

--------------

In addition to this issue, I still have the issue with acreage not sorting correctly.

Please advise.

#611644

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - the results you are getting is expected one since your custom field "Listing price" contains commas that means it is not a numeric value but a string value, for filter and custom field for your listing price must be setup as numeric field.

Have you changed your "Listing Price" field type to numeric or removed the comma now?

#611648

I switched some things up, but there is still an issue.

First, yes the field is numeric. I am no longer displaying a drop down, I am using a free form search for price.

Second, none of my entries have commas or decimals ... They are all just numbers.

The issue:

I can't control what a person types for a search...They are likely going to put commas in their price entries. When commas are used, no results are found.

hidden link

Notice price min and price max.

#611690

Minesh
Supporter

Languages: English (English )

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

Ok I understand what you mean.

So, the alternate way is to remove the comma from the input before we pass the value to query. You can do that using view's filter: wpv-filter_query

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

If you do not know how to do that, I need access details and I'm happy to fix this on your behalf. But I need one test case example with input that what values we should use as input and what should be the expected results. It will help me for testing and to share your required solution.

#612098

Minesh
Supporter

Languages: English (English )

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

Could you please check now.

I've added following code to your said filter.php file:

add_filter( 'wpv_filter_query', 'func_customize_view_query', 10, 3 );
function func_customize_view_query( $query_args,  $view_settings, $view_id  ) {
   
  if ($view_id==1095){
       
	
	  if(!empty($_GET)){
		  
		$min = str_replace(",","",$_GET['wpv-wpcf-property-listing-price_min']);
		$max = str_replace(",","",$_GET['wpv-wpcf-property-listing-price_max']);
	   
	    foreach((array)$query_args['meta_query'] as $k=>$v):
            if(isset($v['key']) and $v['key']=='wpcf-property-listing-price'){
				unset($query_args['meta_query'][$k]['value']);
                                       
                        $query_args['meta_query'][$k]['value'] = "$min,$max";
                              
         }
        endforeach;
	
	}
  }
   
  return $query_args;
 
}

And now I can see that search is working even when you input the comma with price_min and price_max values.

#612204

Minesh,

This solution works wonderfully. Thank you so much for dedicating some time to this.

Have a wonderful weekend!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.