Skip Navigation

[Resolved] Best way to exclude single items from Search inputs

This support ticket is created 3 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/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by smileBeda 3 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#1981761

Hi there, I am fiddling around with some Views Code to produce a few "How To" technical posts showing how to achieve certain things that Views cannot do out of the box.

One of the classic things is for example this:
- we have few posts tagged with several tags (or taxonomies, or fields, whatever)
- we have a custom front end search by those tags, or taxonomies
- we want to exclude just one of those tags from both the search and the view results.

This could be done with a Query filter but as we know, as soon there is a Custom Search that Query filter will be overwritten by the Custom Search.

So a PHP snippet is needed to exclude our "special tag" from the View results.
Easy enough:

add_filter( 'wpv_filter_query', 'remove_snowflake_tags', 101, 3 );
 
function remove_snowflake_tags( $query_args, $view_settings, $view_id ) {

    $types = (array) $query_args['post_type'];
    $working_view = 4024;
    $post_type = 'your_post_type';
    $query_arg_to_add = 'tag__not_in';
     $items_to_exclude = [37];

    if ( !is_admin() && in_array( $post_type, $types ) && $view_id == $working_view ) {
        $query_args[$query_arg_to_add] = $items_to_exclude;
    }
    return $query_args;
}

This code will successfully remove Tag ID 37 from the View Results even if we have a Custom Search for Tags.

However, the Custom Search will still show the Tag with ID 37, albeit with a "results count" of 0, since the query returns no posts with that tag.

So, to remove the tag from the actual search input as well, I had to use "Show only filter options that would produce results" in the View search settings.
But, this option has a warning:
"This can have a performance impact."

In WP Grid Builder for example the search inputs options are for example "read" from the results - so we never need to worry about that, we just exclude things from the query and the search inputs will automatically be reflecting that.

What is the best way in Toolset to do the same?
Apart from the obvious setting, which seems to open up potential performance issues (I assume on large amount of posts only, as I saw no problems in my tiny site)
I wasn't sure if there is a WP Views Filter for it, it does not seem so. Or am I missing something?

The very bad thing with all this is, when we have an AJAX view and "Clear all filters" then our tag comes back!
This is kind of not the goal, obviously.
You can see what I mean here hidden link.
You can see there is no dev_tag.
Then you search, and "Clear all filters"
The tag will be back!
Now reload the page, and the tag will be gone again 😉

This is surely not how it is expected to work, I assume there are 2 problems:
First, the query filter PHP code does not change the actual query inputs but only the results (which is the reason why we still see the tag in the search but with result count 0)
Then, if we use AJAX there seems to be an issue with the PHP Filter not being fired at all when we "Clear all filters"

Thanks for any input 🙂

#1981769

Hmm ignore the second part with AJAX, it is because of !is_admin() part.
This will effectively exclude the code from AJAX, since AJAX runs in the is_admin()

However the part with the search input question still is valid 🙂

#1981901

Hi Beda,

Thank you for contacting us and I'd be happy to assist.

To exclude specific term's option from appearing in the Tag (or any taxonomy) search field, you can use some custom CSS code:


select[name="wpv-post_tag"] option[value="tag-x"] {
display:none;	
}

Note: Please replace "tag-x" with your target term's slug.

The CSS code will make that option hidden, but if you'd prefer to remove it, you can alternatively use some script:


jQuery( document ).on( 'js_event_wpv_pagination_completed js_event_wpv_parametric_search_form_updated js_event_wpv_parametric_search_results_updated ready', function( event, data ) {
/*
enclosed lines will execute in all these events:
-the AJAX pagination completes
-the AJAX search form is updated
-the AJAX search results are updated
-the normal page loading completes
*/
	jQuery('select[name="wpv-post_tag"] option[value="tag-x"]').remove();
});

Since this script is covering all possible AJAX-based events which can update the search form, the part to remove the option will always execute.

regards,
Waqar

#1982395

Ok thanks Waqar for confirming there’s no PHP filter I could use
I knew about The JS and CSS solutions - actually before posting I googled the issue and found topics I had replied myself back in time with those possible approaches too, however I was wondering if there would be a php filter, which now is clear there isn’t

Thanks!