Skip Navigation

[Resolved] Excluding a taxonomy from a view using a filter hook

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

Problem:
For a Post View and/or Custom Search View, how can we exclude a taxonomy term from it using a filter hook?

Solution:
Method 1: Use the “Taxonomy filter” checkbox options in the View to exclude / include terms - screenshot here:
https://d7j863fr5jhrr.cloudfront.net/wp-content/uploads/2017/07/552238-Method_1.png?x71388

Method 2: Achieve this through code using 'wpv_filter_query' something like given below, please note you would need to modify it for your usage.

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

==> Whereas 'book-author' is the Taxonomy name and 'adam' is the slug of the term that you want to exclude.

Relevant Documentation:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
- https://toolset.com/forums/topic/exclude-some-taxonomy-terms-from-filter-search/#post-204079

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.

This topic contains 5 replies, has 2 voices.

Last updated by lolaz 4 years, 4 months ago.

Assigned support staff: Noman.

Author
Posts
#552024

I've created a view (id 10410) and ive added a query filter to include specific taxonomies. I now want to add another query filter to exclude a single taxonomy (id 427). If I understand correctly I have to do it via a filter hook as the taxoniomy option is no longer available via the view interface since I've used it already. I've tried the following but with no luck

add_filter( 'wpv_filter_taxonomy_query', 'exclude_specific_terms_func', 99, 3 );

function exclude_specific_terms_func( $tax_query_settings, $view_settings, $view_id ) {
if($view_id != 10410) return $tax_query_settings;
$tax_query_settings['exclude'] = array(427);
return $tax_query_settings;
}

Could you let me know where the issue is please. Thanks.

#552086

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting Toolset support. You can use the conditional output in the Loop output editor of the View, something like this:

<wpv-loop>
          	[wpv-conditional if="( '[wpv-taxonomy-slug]' ne 'adam' )"]
				
	<li>[wpv-post-body view_template="Loop item in Book Authors - Taxonomy View"]</li>
          [/wpv-conditional]
</wpv-loop>

==> Please replace ‘adam’ with your taxonomy term slug in the above conditional.

Alternatively, you can exclude taxonomy terms and posts using wpv_filter_query hook, the example code and method is given here: https://toolset.com/forums/topic/exclude-some-taxonomy-terms-from-filter-search/#post-204079

OR using wpv-before-display-taxonomy hook:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv-display-taxonomy

Thank you

#552137

Thanks for you reply, but I dont see how using conditional output in the loop solves my issue. My loop query must show posts from category x AND exclude posts from category Y, not show either one or the other.

I have tried to use wpv_filter_query hook as below without success


add_filter( 'wpv_filter_taxonomy_query', 'exclude_specific_terms_func', 99, 3 );

function exclude_specific_terms_func( $tax_query_settings, $view_settings, $view_id ) {
if($view_id != 10410) return $tax_query_settings;
$tax_query_settings['exclude'] = array(427);
return $tax_query_settings;
}

Perhaps you could shed some light on why this code isnt working?

#552208

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Ok I am checking your code, I think there will be another code approach to do that. I am working on it and will update you with the result.

Thank you

#552238

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Method 1.png

‘exclude’ argument is not supported by WP anymore, you will need to use operator 'NOT IN' instead of exclude. There are 2 possible methods for this at moment

Method 1: Use the “Taxonomy filter” checkbox options in the View to exclude / include terms -- screenshot attached.

Method 2: Achive this entirely through code like given below, without any Taxonomy filter in the View.

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

==> Whereas 'book-author' is the Taxonomy name and 'adam' is the slug of the term that you want to exclude.

We cannot use View filter and custom code over it at the same time, based on the information I have about your setup at moment. Thank you

#552399

Thank that query hook worked to exclude the required categories. I did use it i combination with the taxonomy filter in the view, which was set to include some category terms.