Skip Navigation

[Resolved] Exclude several taxonomies from a taxonomy view

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

Problem:
Exclude several taxonomy terms from a taxonomy view

Solution:
You can use view's hook "wpv_filter_taxonomy_query" in order to filter your taxonomy view query on fly.

You can find proposed solution, in this case with the following reply:
https://toolset.com/forums/topic/exclude-several-taxonomies-from-a-view/#post-1155347

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_query

This support ticket is created 6 years, 1 month 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by SimonP3753 6 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1154949

Tell us what you are trying to do? Exclude several taxonomies from a view

Is there any documentation that you are following? https://toolset.com/forums/topic/excluding-a-taxonomy-from-a-view-using-a-filter-hook

I just need to know exactly where to put this code please, forgive the lack of knowledge on this I'm sure it's very simple..

Thanks

Simon

#1155228

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - you should try to add such code to your current theme's functions.php file.

If you do not wish to add it to functions.php file then Toolset offers also "Custom Code" section at:
Toolset => Settings => Custom Code

More info:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

#1155284
Screen Shot 2018-11-29 at 08.54.11.png
Screen Shot 2018-11-29 at 08.54.22.png
Screen Shot 2018-11-29 at 08.55.54.png
Screen Shot 2018-11-29 at 08.56.05.png

Hello there

I must be doing something wrong here... I've attached screenshots.

This line of code: add_filter( 'wpv_filter_query', 'exclude_terms_func', 10, 3 );

Should I be changing those numbers to something?

Many thanks

Simon

#1155302

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

No - but I think I spot the issue with the code you shared with the following image
=> hidden link

Please - edit your taxonomy and replace taxonomy slug with 'taxonomy' argument and same thing you should do by editing your terms and copy the slug and replace the slug there with 'terms' argument and try to resovle your issue.

#1155305

Hi Minesh

I don't understand properly sorry.

Is that all to be done in the php code snippet?

Or am I editing the actual taxonomy somehow as well?

Don't suppose you could fix my code please? 🙂

{code}add_filter( 'wpv_filter_query', 'exclude_terms_func', 10, 3 );
function exclude_terms_func($query, $setting, $views_ID)
{
if($views_ID == 7453) // your view id
{
$query['tax_query'][] = array(
'taxonomy' => 'Composers-Full-Names', // taxonomy name
'field' => 'slug',
'terms' => array( 'Anonymous ()', 'anonymous-16-17th-c' ), // term slug for exclude
'operator' => 'NOT IN'
);
$query['tax_query']['relation'] = 'AND';
}
return $query;
}
{/code}

#1155314

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check my comments in following code and adjust the code where needed and try to resolve your issue.

add_filter( 'wpv_filter_query', 'exclude_terms_func', 10, 3 );
function exclude_terms_func($query, $setting, $views_ID){
if($views_ID == 7453) // your view id
{
$query['tax_query'][] = array(
// taxonomy name  - please check the taxonomy slug is correct otherwise check it and adjust it accordingly
'taxonomy' => 'composers-full-names', 
'field' => 'slug',
// check Anonymous () term slug and adjust it accordingly, anonymous-16-17th-c slug is OK
'terms' => array( 'Anonymous ()', 'anonymous-16-17th-c' ), // term slug for exclude
'operator' => 'NOT IN'
);
$query['tax_query']['relation'] = 'AND';
}
return $query;
}
#1155321
Screen Shot 2018-11-29 at 09.54.46.png

Hi

I can't get it to work sorry.

For taxonomy name I've tried the name singular, plural and also it's slug.

{code}
add_filter( 'wpv_filter_query', 'exclude_terms_func', 10, 3 );
function exclude_terms_func($query, $setting, $views_ID){

if($views_ID == 7453) // your view id
{
$query['tax_query'][] = array(
// taxonomy name - please check the taxonomy slug is correct otherwise check it and adjust it accordingly
'taxonomy' => 'composer-long',
'field' => 'slug',
// check Anonymous () term slug and adjust it accordingly, anonymous-16-17th-c slug is OK
'terms' => array( 'anonymous-16-17th-c' ), // term slug for exclude
'operator' => 'NOT IN'
);
$query['tax_query']['relation'] = 'AND';
}
return $query;
}
{/code}

What could it be??

Ultimately I need to exclude about 25 terms in total.

Thanks

Simon

#1155334

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

OK - so it will be easy for you if you can send me wp-admin access details and problem URL where you added your view.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1155347

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - that is why for good reason I asked for access details.

You were using the taxonomy view so the correct code to exclude those terms as given under:

add_filter( 'wpv_filter_taxonomy_query', 'func_exclude_specific_terms', 10, 3 );
function func_exclude_specific_terms( $tax_query_settings, $view_settings, $view_id ) {
  if($view_id == 7453) { // your view id
    $tax_query_settings['exclude'] = array(1963,2471);
    
  }
  return $tax_query_settings;
}

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_query

I see it in action and it works just fine - could you please confirm:
=> hidden link

#1155367

My issue is resolved now. Thank you!

As some others have noted on other similar threads: a filter option to exclude by front end checkbox would be good (the functionality to include by check box is possible but tiresome when you have many terms).

Thanks again.

Simon

#1167657

Hi Minesh

I'm not sure why but the code has stopped working and the anonymous and various terms are appearing again.

hidden link

There has been some change in the views loop editor.

Here's the PHP code:

{code}
add_filter( 'wpv_filter_taxonomy_query', 'func_exclude_specific_terms', 10, 3 );
function func_exclude_specific_terms( $tax_query_settings, $view_settings, $view_id ) {
if($view_id == 7453) { // your view id
$tax_query_settings['exclude'] = array(3410,3575,3641,3348,3079,3280,3694,3397,3636,3264,3204,3160,3557,3395,3026,3665,3399,3724,3687,3645,3284,3487,3183,3184,3116,3373,3597,3287,3288,3076,3407,3075,3605,3379,3393,3599,3119,3606,3144,3188,3027,3074,3592,3608,3609,3679);

}
return $tax_query_settings;
}
{/code}

And the loop:

{code}
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop>
<p>[wpv-taxonomy-link]</p>
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]
{/code}

Many thanks

Simon