Skip Navigation

[Resolved] Searching a field for multiple values

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

Problem: I would like to allow my site visitors to search for posts, filtering by custom field value. That filter should be a text input, and should support multiple values in a comma-separated list format.

Solution: Use the "IN" operator in this custom field Query Filter, and choose "Text input" for the type of control in display options.

Relevant Documentation:
https://toolset.com/documentation/user-guides/views/filtering-views-by-custom-fields/

This support ticket is created 4 years, 9 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by tisonK 4 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1566935

Tell us what you are trying to do?

The client needs to search one field for multiple values. (Specifically they want to search the Listing_ID field for a few ID's, preferably by submitting a comma-delimited, or space delimited list).
Using the multi-select functionality won't work because we'd have to load 60K ID's into the dropdown, it would crush the page performance.

Another idea - is it possible to parse the search field before it goes to the db? Is there a filter I can use to do that before a search is made?

Is there any documentation that you are following?
No I can't find any on this.

Is there a similar example that we can see?
If you do a search on a search engine - you can search for multiple words at once.

What is the link to your site?
hidden link

#1567315
Screen Shot 2020-04-05 at 9.09.47 AM.png
Screen Shot 2020-03-29 at 12.05.13 PM.png

The client needs to search one field for multiple values.
You can set this up using a Query Filter configured to use the "IN" operator, and a text filter input. See the screenshot here for the Query Filter configurations. If you cannot see the Query Filter panel in your View editor screen, scroll to the top right corner and click "Screen Options" to enable the Query Filter panel.

Here is an example of the filter control shortcode:

[wpv-control-postmeta type="textfield" field="wpcf-book-single-line-1" url_param="wpv-wpcf-book-single-line-1"]

Your client can enter comma-separated values in the filter input to search for posts with custom fields matching any of those values.

Another idea - is it possible to parse the search field before it goes to the db? Is there a filter I can use to do that before a search is made?
Yes, there is a PHP API specifically for this called wpv_filter_query:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Click "More usage examples" to see some PHP examples using this filter.

#1568523

Christian,
Ok this is great, I think this will work.

One question on the wpv_filter_query:
Am I able to call this on page load (before a search is made?). I would like to setup some filters that are in the search by default (that are not going to be controlled by the user).

I tried echoing out the meta_query param and it's empty on page load (works great after a search is made though),

Any ideas/help is huge thank you!
T

#1568551

Am I able to call this on page load (before a search is made?). I would like to setup some filters that are in the search by default (that are not going to be controlled by the user).
Yes, you can use wpv_filter_query to manage the results before the page is loaded, but it's usually easier to set these filters in the Query Filter section of the View editor screen if possible. If you cannot see the Query Filter, scroll to the top right corner of the View editor screen and click "Screen Options" to enable the panel.

#1572083

Christian,
This is super helpful, I hope to have this tested today, more very shortly,
T

#1572639

Great, I will stand by for your update. I am not available on Fridays or Saturdays but I will return Sunday. If you need assistance before then feel free to create another ticket.

#1572663

Christian,
Having a tough time here on one thing:

I'm just trying to remove one of the filters from the top "Query Filter" section. I'm just trying to remove one of the filters and it's giving me an error, saying:

"Only lowercase letters, numbers, hyphens and underscores allowed as URL parameters".

For virtually every field that was added up on that top section (roughly 30 filters up there).

What is odd is - I didn't manually add any of the query params to this top section (I used toolset to add them). Now it's telling me to change them all to lowercase - won't this now break the query functionality?

Here's what I'm seeing now:
hidden link

How do I get around this error message?
Any thoughts?

Thanks again,
T

#1573109

Christian that worked! You rock!

The "IN" query filter worked great w/ commas.

Also I was able to get around this last issue and save the query filters.