Tell us what you are trying to do?
I am using Views and Access to control some search results. Based on user role, the user sees various custom fields associated with a "lab-tests" post type. "Owners" can see all fields associated with a post type; everyone else can only see a limited number of fields.
The search box only searches title, content, and excerpt. I want the search box to search through custom fields as well, as having that limited search functionality seems unintuitive. So I imported all the custom field values into the content body of each single post.
Say a lab-test has a field, "afg-cost", that only "owners" can view. Let's say that a lab-test has an "afg-cost" of $250. If a non-owner searches for $250 in the search field, any lab-test that has an "afg-cost" of $250 will show up in the search results, even if the user can't actually see that field.
I would like to be able to remove search results from the array of results before the user sees them via conditional JS logic.
Is there a front-end javascript event that I can do to alter the array of search results before they hit the DOM?
What is the link to your site?
testmenu.vmgagencysites.com
Hi Jeff,
Thank you for contacting us and I'll be happy to assist.
As for your original question, you can bind your custom JavaScript function to execute before or after the filtered search results are shown.
In your view's "Custom Search Settings" section, you'll find some fields to bind custom JavaScript function names for execution.
( screenshot: hidden link )
Based on what you've shared, a better alternative is to use the "Relevanssi" plugin ( https://wordpress.org/plugins/relevanssi/ ), which can extend the native WordPress search functionality, so that it can include data from the custom fields as well (without making them part of the content).
We have a detailed guide on the topic at:
https://toolset.com/documentation/user-guides/searching-texts-custom-fields-views-relevanssi/
I hope this helps.
regards,
Waqar
Waqar,
Thanks for your reply. Using Relevanssi, would I be able to:
- Produce search results that search Types custom fields as well as the title, content, and excerpt fields?
- Search different fields based on user role?
Thank you.
Hi Jeff,
Thanks for writing back.
> Produce search results that search Types custom fields as well as the title, content, and excerpt fields?
- As per the Relevanssi plugin's documentation, it extends the WordPress search to include text from custom fields, titles, and content. I'm not 100% sure about its handling of excerpts, but for most accurate and up-to-date information about plugin's features, I'll recommend getting in touch with Relevanssi's official support team.
> Search different fields based on user role?
- The Relevanssi plugin extends WordPress' native search feature globally, which means you'll be able to set its option for everyone and not based on individual user role.
regards,
Waqar
Waqar,
Thank you.
From what I can tell, there are two ways to resolve my issue.
<ol>
Perhaps create two different search and filter options (two separate [wpv-filter-start][wpv-filter-end] tags, and display them to the page via Access shortcodes?
Access the array and manually strip unwanted results with custom javascript.
</ol>
For the second one -- i realize that I use custom javascript here:
hidden link
But what variable can I use to access the actual array of results that is being populated on the page? I don't see it anywhere in your documentation, and I couldn't find it while looking through the plugin code itself.
Hi Jeff,
Your understanding is correct, but I'll choose the first approach, over the second one, for the following reasons:
1. From the performance's perspective with the second approach, you'll be calling more data (results) than actually needed from the database and then to remove/hide the unwanted results, you'll be performing even more processing (DOM manipulation) through JavaScript.
2. If you'll have pagination turned on for your results with the second approach, there will be a difference in the number of results shown on each page to visitors. The actual loop of each page will return X number of results, but after your script will run to remove/hide the unwanted results, that number will change for the visitor.
Based on the above points, it will be better to conditionally segment the results generation (approach 1), compared to segmenting them after generation (approach 2).
Note: there are no variables available for the results that you can use in your custom JavaScript function, but you can use DOM manipulation (for example include certain ID or class with each result's container, to target them through a custom script).
I hope this clarifies and let me know if you have any question around these points.
regards,
Waqar