Skip Navigation

[Resolved] Excluding a specific category from posts view with front end filter…

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

Problem:
The user would like to exclude posts assigned to a certain taxonomy term. And also be able to use a filter on the same taxonomy.

Solution:
This will require custom code such as:

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;
}

Relevant Documentation:

This support ticket is created 3 years, 7 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: Africa/Casablanca (GMT+01:00)

This topic contains 15 replies, has 2 voices.

Last updated by riccardoU 3 years, 7 months ago.

Assisted by: Jamal.

Author
Posts
#2160303
Screenshot 2021-09-04 at 01.05.58.jpg
Screenshot 2021-09-04 at 01.00.20.jpg

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.

#2160323
Screenshot 2021-09-04 at 01.08.41.jpg

I just saved and I can see it reset the content selection to this...

#2160685

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.

#2161363

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?

#2161921

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.

#2161935

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.

#2167273

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

#2167753

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.

#2172885
Screenshot 2021-09-18 at 21.43.11.jpg
Screenshot 2021-09-18 at 21.43.02.jpg

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.

#2172903

We are talking about the taxanomies of the wordpress POSTS right?

#2173091

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. **

#2173739

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.

#2174037
Screenshot 2021-09-20 at 14.59.20.jpg

if that was the case why is it still showing up in my view?

#2174047
Screenshot 2021-09-20 at 14.59.20.jpg

if that was the issue why is it still not working and I can see that post with news in my view?

#2174153

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.