Skip Navigation

[Resolved] Calculating a value to search/filter on

This support ticket is created 5 years, 1 month 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Simon Logan 5 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1385901

I'm looking to use the values entered into two fields on my form to calculate the value to filter on. So I have a custom field called "ltvrate" which stores one of these values (50,65,75,90) which I am wanting users to be able to search/filter by using a less than or equal to comparison. This works if I offer a text field for them to enter the number into, however what I was looking to do was to allow this value to be calculated for them.

So instead of them directly entering this value (which equates to a % rate) I would get them to enter two other numerical amounts (loan amount and project total) which would then be used to calculate the % for them. Therefore what I'm looking to do is to effectively hide the "ltvrate" field and have its value calculated when they submit the form based on the values in the loan_amount and project_total text fields instead. The value in the ltvrate would still be used to filter the results, however.

FYI the site is development-locked just now but I can supply access details if this helps.

#1385921

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Simon

Using calculated values for filtering like this isn't a supported feature, so you'll need to customise the filtering somewhat to get what you want.

It seems to me you have two choices.

The first would be to include form inputs for the two text fields that will be used to calculate the desired field.

Use JavaScript (or jQuery, which will be available on the page) to add event listeners to these two fields so that when they lose focus you perform the calculation and update the value of the target field with the result. (You can hide that field if you want with CSS.)

Then when the form is submitted the computed value will already travel with the form.

An alternative would be to have the two fields included in the search form, submit the form, and then handle everything with PHP on the server.

Use the wpv_filter_query API hook to modify the query arguments to include the calculated value of the target field in the query. Where you insert the filter controls in the Search and Pagination section (that are normally added with the shortcodes wpv-control-postmeta—for custom fields—and wpv-control-post-taxonomy—for taxonomy terms) you can use the shortcode wpv-control to insert the UI for arbitrary fields which will be submitted along with the search form but are not used in building the post query, which you can access via the $_POST object and use to include a postmeta query with the calculated value.

That implies more working knowledge of WordPress queries, as well as some PHP, whereas the first option is a pure JavaScript option, so it rather depends on which you are more comfortable with.

#1386191

Thanks for that, the J/S approach was the one I was planning on taking so will give it a go.