Skip Navigation

[Resolved] Filter View

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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by fahimS-2 1 year, 1 month ago.

Assisted by: Waqar.

Author
Posts
#2537809
Search Page.jpg
Search Page Backend.jpg
Rating Field.jpg

Tell us what you are trying to do?
I am creating a view filter. I have a custom Number field called rating. Now in the filter I set the condition Greater than or equal to. I have one post and it has 4.1 rating. Now in the front end if I put 4 in the search box and search, the view shows the post. But if I put 4.1 in the search box and search, the view doesn't show the post.

Is there any documentation that you are following?
No

Is there a similar example that we can see?
No

What is the link to your site?
hidden link

#2538397

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

By default, the "number" type custom field is designed to work with whole numbers and not decimal numbers.

To change the comparison type to 'decimal' in the view's filter for this particular field, you can use the 'wpv_filter_query' filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query


add_filter( 'wpv_filter_query', 'filter_rating_decimal_precision', 1000 , 3 );
function filter_rating_decimal_precision( $query_args, $view_settings ) {
	if ( !is_admin() ) 
	{
		if( !empty($query_args['meta_query']) ) {
			for ($i=0; $i < (count($query_args['meta_query'])-1) ; $i++) { 
				if ( $query_args['meta_query'][$i]['key'] == 'wpcf-property-rating' ) {
					$query_args['meta_query'][$i]['type'] = 'DECIMAL(10,3)';
				}
			}
		}
	}
	return $query_args;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

This function will change the comparison type to 'decimal' for the field with slug 'property-rating', in all views.
( you'll change the 'property-rating' with the slug of the field used on your website )

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2538449

Hey, thanks for your great response. I have couple of questions.

1. The slug of my Number Custom Field is 'rating'. Do I need to change 'wpcf-property-rating' to 'wpcf-rating' in the code?

2. Do I need to change anything else in the code? I found 'wpcf-property-rating' just once in the code.

3. Also should I close the php in my newly created Custom Code Rating.php?

4. Also can you kindly tell me what are Run context: Front-end , WordPress admin & AJAX calls. I tried switching them on and off to see the effect on the result . But I couldn't find any effect.

Once again thanks for your help.

#2538507

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back and glad I could help

> 1. The slug of my Number Custom Field is 'rating'. Do I need to change 'wpcf-property-rating' to 'wpcf-rating' in the code?
- The prefix 'wpcf-' is appended to all the custom fields registered through the Toolset Types plugin.

So, if in the field's settings, you have set the slug to 'rating', in the code, you'll use 'wpcf-rating'.

> 2. Do I need to change anything else in the code? I found 'wpcf-property-rating' just once in the code.
- Yes, there is just one instance of this field slug that you need to change in the code.

> 3. Also should I close the php in my newly created Custom Code Rating.php?
- No, you don't have to include the closing PHP tag.

> 4. Also can you kindly tell me what are Run context: Front-end , WordPress admin & AJAX calls. I tried switching them on and off to see the effect on the result . But I couldn't find any effect.
- The "Run context" determines where a particular code snippet is included for execution.

a). Front-end: when any frontend page/post is viewed/rendered
b). WordPress admin: when any backend or admin area page/section is viewed/rendered
c). AJAX calls: when any AJAX call is made by the WordPress

For this particular code snippet, you can keep all 3 contexts enabled.

I hope this explanation helps and let me know how it goes.

#2538633

My issue is resolved now. Thank you!

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