Skip Navigation

[Resolved] using conditional statement within Views [wpv-control-postmeta] shortcode

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

Problem:

I want the user to be able to filter view results by: =GCPEA or !=GCPEA

Solution:

You can try wpv_filter_query filter hook.

Relevant Documentation:

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

This support ticket is created 2 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 12 replies, has 2 voices.

Last updated by JeremyM3920 2 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2122825

My Resource Library at hidden link displays the Resources custom post type with user selected filters. The 'All Organizations' filter (second one listed inline) should filter by the 'wpcf-doc_organization-author' custom field via 'url_param="wpv-wpcf-doc_organization-author"' I would like the ability to allow the User to select from three fields in the Select dropdown: "All Organizations" (everything), "GCPEA" (only those with GCPEA as the meta value), and "Other Organizations" (only those NOT with GCPEA as the meta value).

I tried different code formats to try to get this functional but I'm unable to. My latest code for that filter is:

[wpv-control-postmeta values=",GCPEA,'( $(wpcf-doc_organization-author) ne 'GCPEA' )'" display_values="All Organizations,GCPEA,Other Organizations" field="wpcf-doc_organization-author" type="select" source="custom" url_param="wpv-wpcf-doc_organization-author"]

Would you be able to help me get this functional? Thank you in advance!

#2123127

Hello,

There isn't such kind of built-in feature within Toolset Views/Blocks plugin, see our document:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-control-postmeta
You can not use conditional statement within Views [wpv-control-postmeta] shortcode.

But you can enable option "Show only available options for each input" in section "Custom Search Settings", see my screenshot options.JPG.

Above option will show only available options by view's result

#2123491

Thanks for letting me know. I didn't see a screenshot attached to your message. Could you share with me, please?

#2124849
options.JPG

I have uploaded the screenshot options.JPG, please check it

#2125571

To confirm, are you saying there's no workaround for the ability to allow the User to select from three fields in the Select dropdown: "All Organizations" (everything), "GCPEA" (only those with GCPEA as the meta value), and "Other Organizations" (only those NOT with GCPEA as the meta value)?

#2125933

As I mentioned above, you can not use conditional statement within Views [wpv-control-postmeta] shortcode, if you want to limit the options by available results, you can try the option "Show only available options for each input".

#2125941

So how do I limit options by one value and the other being a single value that represents all other values that are not the first value?

#2126067

To avoid more misunderstanding, please elaborate the question with more details:
So how do I limit options by one value and the other being a single value that represents all other values that are not the first value?

What single value do you want to represent? How do you setup the custom fields in posts?

The option "Show only available options for each input" depends on the search result, it will show search options which are available in the search result, and hide search options automatically which does not present in the search result, see my screenshot:
https://toolset.com/wp-content/uploads/2021/07/2124849-options.jpg

And in shortcode [wpv-control-postmeta] you can setup default option "All" with the attribute "default_label", like this:

[wpv-control-postmeta type="select" field="wpcf-doc_organization-author" default_label="All" url_param="wpv-wpcf-doc_organization-author"]

More help:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-control-postmeta
default_label (opt):
'default option text'
Add default option like All or Any on taxonomy select control.

#2126893

What I want to do is provide two Select options for the 'wpcf-doc_organization-author' custom field: one that contains a meta value of "GCPEA" author and one that contains all other values that are NOT "GCPEA" author. In other words, I want the user to be able to filter by: =GCPEA or !=GCPEA.

I understand that I cannot have a conditional rule within the [wpv-control-postmeta] shortcode, but I wanted to ask if you were familiar with a workaround that would provide me with what I'm looking for? Thank you in advance!

#2127393

For the question:

In other words, I want the user to be able to filter by: =GCPEA or !=GCPEA

Yes, it is possible with custom codes, for example:
After user submit the search form, you can use wpv_filter_query to trigger a custom PHP function:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
In this PHP function, get the wpcf-doc_organization-author input value:
hidden link
If it is "!=GCPEA", then setup view's query to what you want:
https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

#2131577
Screen Shot 2021-08-02 at 8.51.25 AM.jpg

Thank you for this info. Last question: would these custom codes work if the View filter's custom search settings is "AJAX results update when visitors change any filter values" rather than requiring visitors to click the Search button?

#2132035

Yes, custom codes with "wpv_filter_query" filter hook will work when option "AJAX results update when visitors change any filter values" is enabled.

#2132043

Thank you for your help!

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