Tell us what you are trying to do?
I am trying to filter the specific category "coming soon" to be displayed in the posts view. Despite selecting "coming soon" I see all posts from all categories....
Please see screenshot of the front end filter. I would like to ONLY show "coming soon" in the query.
I just saved and I can see it reset the content selection to this...
Hello and thank you for contacting Toolset support.
Because you have a search filter on categories, you cannot have a separate query filter on categories too. Only one of them can exist on the view.
Let me ask, you want to display the posts from the "coming soon" category, right?
If that's the case, just remove the category search filter, save the view, then recreate the query filter similar to your screenshot "Screenshot_2021_09_04_at_01.05.58.jpg"
However, based on the title of this thread "Excluding a specific category from posts view with front end filter...", I am keen to think that you want to have a view that displays posts from all the categories except "coming soon", is that right?
Please let me know your answers so I can better advise you.
I would like to seperate "coming soon" visually from published (all other posts excl. coming soon).
I get that by removing the search filter, i can get "coming soon" to be displayed.
The question is rather, how do i display all remaining posts excluding that "coming soon / taxanomy" by still allowing the search?
P.S. I could possibly use a different block to display "coming soon" - the question remains though how to exclude coming soon if i want the search displayed too?
the question remains though how to exclude coming soon if i want the search displayed too?
This cannot be done only through the user interface. It will need custom code.
First, create the view with the search filters, which will let the user search for posts in all categories.
Then, through custom code, we can exclude the category "coming-soon" from the results by changing the view's query arguments. We'll need to use the wpv_filter_query hook to exclude the coming-soon category. Read more about the filter here https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Check an example in the summary of this similar ticket https://toolset.com/forums/topic/excluding-a-taxonomy-from-a-view-using-a-filter-hook/
The view is by default configured to display only the filter that should reproduce results. So, the "coming-soon" category won't appear on the search filter.
the question remains though how to exclude coming soon if i want the search displayed too?
This cannot be done only through the user interface. It will need custom code.
First, create the view with the search filters, which will let the user search for posts in all categories.
Then, through custom code, we can exclude the category "coming-soon" from the results by changing the view's query arguments. We'll need to use the wpv_filter_query hook to exclude the coming-soon category. Read more about the filter here https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Check an example in the summary of this similar ticket https://toolset.com/forums/topic/excluding-a-taxonomy-from-a-view-using-a-filter-hook/
The view is by default configured to display only the filter that should reproduce results. So, the "coming-soon" category won't appear on the search filter.
Where do input that code?
add_filter( 'wpv_filter_query', 'exclude_terms_func', 10, 3 );
function exclude_terms_func($query, $setting, $views_ID)
{
if($views_ID == 3346) // your view id
{
$query['tax_query'][] = array(
'taxonomy' => 'book-author', // taxonomy name
'field' => 'slug',
'terms' => array( 'adam', 'adam2016' ), // term slug for exclude
'operator' => 'NOT IN'
);
$query['tax_query']['relation'] = 'AND';
}
return $query;
}
I know i will have to change the terms to coming-soon
You can add the code to a Toolset snippet in Toolset->Settings->Custom code. Read more about it here https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
Make sure that the snippet is active. And adapt all of the following lines:
- Line 5: Change 3346 with your view's ID.
- Line 8: Change 'book-author' with the taxonomy slug.
- Line 10: Change the terms with your terms' slugs.
I added the code
<?php
/**
* New custom code snippet (replace this with snippet description).
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
add_filter( 'wpv_filter_query', 'exclude_terms_func', 10, 3 );
function exclude_terms_func($query, $setting, $views_ID)
{
if($views_ID == 222) // your view id
{
$query['tax_query'][] = array(
'taxonomy' => 'news', // taxonomy name
'field' => 'slug',
'terms' => array( 'category', 'news' ), // term slug for exclude
'operator' => 'NOT IN'
);
$query['tax_query']['relation'] = 'AND';
}
return $query;
}
as you can see in the screenshot but the news post is still being displayed.
We are talking about the taxanomies of the wordpress POSTS right?
Would you allow me temporary access to your website to check this further? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
Well, the only issue I could spot is the view ID, it is 15057 instead of 222. You can get the view's ID from the browser developer tools as follow hidden link
I hope this helps. Let me know if you have any questions.
if that was the case why is it still showing up in my view?
if that was the issue why is it still not working and I can see that post with news in my view?
Well, regarding the post "News Test", it was just a cache issue. Once I purged the cache the post did not appear anymore.
Regarding the "News" category on the dropdown, the view is configured to display all possible values. It needs to be configured to return only the values that will produce results. Check this screenshot hidden link
Please check now and confirm if it is resolved. If you need assistance with something else, please create a new ticket.