I have a view with a custom search field for standard tags on a CPT. It works. But now, I would like to enhance its behaviour to be able to include posts with some tags and exclude some with other tags. I can do JS for the frontend solution I have in mind but don't know how to control the search for that with JS, PHP or any other way.
Is there any documentation that you are following?
the regular documentation does not seem to cover that
Is there a similar example that we can see?
LinkedIn Sales Navigator advanced search - hidden link
Hello. Thank you for contacting the Toolset support.
So basically you want to control your taxonomy tags filter to display few options that you want and hide few options that you do not want to display with the filter.
If that is correct, views offer the filter hook: wpv_filter_taxonomy_frontend_search_get_terms_args
For example:
add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'func_include_exclude_tags', 10, 3 );
function func_include_exclude_tags( $args, $tax, $view_id ){
if ( $view_id == 99999 && $tax = 'post_tag' ){
$args['exclude'] = array( 1, 2, 3 ); // Edit array of term IDs to exclude
}
return $args;
}
Where:
- Replace 99999 with your original view ID.
Minesh, thanks for the thorough response, it is useful in my context.
However, I believe we have a misunderstanding. If I understood correctly your solution filters the available tags term available in the custom search field for a user to be able to choose from, correct?
However, I need also to be able then to filter the custom posts by tags but in a more complex way where I send a more complex logical expression to the view filter. The view should show posts with some tags and exclude the ones with other tags.
Basically, I would mark the tags terms in my UI with green (optional), blue (mandatory) and res (exclude) then somehow pass them to the view to show posts that have:
1. any of the green
2. all of the blue
3. none of the red
However, I believe we have a misunderstanding. If I understood correctly your solution filters the available tags term available in the custom search field for a user to be able to choose from, correct?
==>
Yes, thats correct.
However, I need also to be able then to filter the custom posts by tags but in a more complex way where I send a more complex logical expression to the view filter. The view should show posts with some tags and exclude the ones with other tags.
Basically, I would mark the tags terms in my UI with green (optional), blue (mandatory) and res (exclude) then somehow pass them to the view to show posts that have:
1. any of the green
2. all of the blue
3. none of the red
==>
Do you mean that you want to pre-filter the view results with some tags?
I checked the video you shared and I see how you are using the LinkedIn text search but Toolset text search obviously do not support such feature, when you use text search it will just be a simple text search, it will not respect the "And" and "Not" clauses with input keyword in text search.
When you say you want to apply some color coding like green, blue and red can you please share how exactly you are aiming to display and pass it to view filter as you can add the taxonomy tag filter as checkboxes, radio or selectbox those are the normal field types using which you can display the taxonomy tag filter on frontend.
1. If you look at grantly.eu/sl/grants (don't mind the language or use auto translate for Slovenian 🙂 ), you can see a quite standard custom search filter on the left.
2. At the bottom, in the last accordion section under 'Oznake', when you open it, you can see tags. Currently, they behave in the standard (the only supported afaik) way - if you select one or more, the calls that contain at least one of the tags are displayed.
3. I could make it so that I mark some tags with green, red or blue. That would add two additional 'functions' to tags: being (a) mandatory or (b) excluded (must not be present in a post).
4. But, how can I then pass this complex filter to the backend? That would mean somehow adding to the standard filter condition which is "has any of the blue tags", also "must have all the green tags" and "must not have any of the red tags". As a sidenote - I can code and can pass these tags in any form to wherever.
I really hope I managed to explain this time. If you have a question, pls refer to a number where I am not clear. Thanks.
Minesh won't be available for a couple of days. If you don't mind, I'll continue with you on this ticket.
I visited the hidden link page and, right now, the tags filter works as usual, you select the tags to filter with, and they are then summarized before the filter with a green background. I could not figure out how you allow to select the tags in different colors. But, I believe, that is not the question. You want to translate the selected tags/colors into the view's words and get the posts that match the criteria, right!
From a Toolset side, the search filters will be search for any selected tags. You can then use the wpv_filter_query filter to programmatically change the query's arguments to suit your needs. They will be used to create the WP_Query instance that will search for the posts and pass them to the view to display. Read more about it here https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
For example:
add_filter( 'wpv_filter_query', 'func_my_custom_search', 10, 3 );
function func_my_custom_search( $query_args, $view_settings, $view_id ){
if ( $view_id == 99999 ){
// your logic hear to alter the query arguments.
// below a hardcoded example.
$query_args['tax_query'] = array(
// Match all the conditions
'relation' => 'AND',
// Any of the following tags
array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => array( 12, 13 ),
),
// exclude the following tags
array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => array( 45, 67 ),
'operator' => 'NOT IN',
),
);
}
return $query_args;
}
I hope this answers your question. Let me know if you have any questions.
Awesome, I'll be glad to help. Let me set this ticket as waiting for your reply, which will keep it open for two weeks. And I'll remain at your disposal.