Skip Navigation

[Resolved] How do I create a view with a filter comparing a custom field?

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)

This topic contains 2 replies, has 2 voices.

Last updated by amandaD 1 year, 9 months ago.

Assisted by: Nigel.

Author
Posts
#2573715
Screenshot 2023-03-15 173844.png

Tell us what you are trying to do?
I have a CPT with a "recommended price" and an "actual price". How do I build a view (in classic mode) that will show me all products with an actual price that is greater than the recommended price?

I've tried using a URL_PARAM but it's not working.

What is the link to your site?
hidden link

#2574063

Nigel
Supporter

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

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

Hi there

The way the filters work you test the post value of some field compared to a static value (even if you grab the static value from a url parameter, or a shortcode attribute).

For example, show all posts with a price field greater than 99 (whether you explicitly provided the value 99, or 99 came from a url parameter or shortcode attribute).

But you are aiming to compare the value of one field with the dynamic value of another field, and WordPress queries don't work like that. (See examples at https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters; you can use a variable for the value you want to compare, but it is still a static value that doesn't change once you invoke the query.)

You could do this by directly writing your own SQL queries, but you wouldn't be using Views for that.

However, all is not lost!

Without such a filter the View is going to return all posts of your CPT.

But you don't have to output them all.

That's where conditional shortcodes come in. Where you output the desired content, wrap that in a wpv-conditional shortcode that tests whether the actual price is greater than the recommended price (both field values coming from the current post in the loop).

If you are using the legacy editor see this page for how to do that: https://toolset.com/documentation/legacy-features/views-plugin/conditional-html-output-in-views/

(Note, pagination won't work as expected if you do this, as the pagination is based on the number of posts returned by the query, not by the number output on the screen.)

#2574097

Great, thanks! I never thought of using conditionals!