Skip Navigation

[Resolved] Hiding Taxonomy Terms From Views

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

Problem:
Hiding Taxonomy Terms From Views

Solution:
To hide specific taxonomy term you can use the WordPress hook pre_get_posts.
=> https://toolset.com/forums/topic/hiding-taxonomy-terms-from-views/#post-2050813

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/hiding-taxonomy-terms-from-views/page/2/#post-2057275

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
- 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 25 replies, has 3 voices.

Last updated by kostaB 3 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2048279

* Tell us what you are trying to do?

I'm trying to hide certain products with a taxonomy term attached to them (early bird) under the custom taxonomy (Garment Types) from certain archive views, such as my default archive as made with Toolset. I want things in the 'garment types' taxonomy to be visible, just not 'early-bird' products; I only want those to be visible in the password protected page where I put another archive view.

* Is there any documentation that you are following?

https://toolset.com/forums/topic/excluding-a-taxonomy-from-a-view-using-a-filter-hook/?fbclid=IwAR0OJm2fnu_fpk6AqZMEXjK5tk03JHyjEPhkFzfrICBjIozavc2MJXfCKtg

I don't see query filters in the archive. And rather than just selecting every taxonomy term except for the one I want to use, I would rather just select the one I DON'T want to use.

I tried to use the above link's code snippet, tested and activated it in Toolset's Custom Code tab. Where it says 'taxonomy name', I tried the slug, the singular or the plural, to no avail.

This is the information relevant to my products archive:

My view ID: 8173

Taxonomy name (plural/singular): Garment Types/Garment Type

Taxonomy slug: garment-type

Term slug: early-bird

Is there anything I missed?

* Is there a similar example that we can see?

I suppose the above support issue is similar.

* What is the link to your site?

It's a dev site, I would rather send it in private,

#2048761

Hello and thank you for contacting the Toolset support.

To better assist you, I might need to access your website and check further.

However, I would like to ask if this about a view or an archive template, I am confused?
Please check our articles on each of them to see the distinctions:
- https://toolset.com/glossary/view
- https://toolset.com/glossary/archive

The code that you are trying will only work for views, it won't work for archives.

Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

Please provide URLs in the backend and frontend of the view/archive you are working on.

#2048839

A similar code to this one should work:

add_action( 'pre_get_posts', 'prefix_hide_term_posts' );
function prefix_hide_term_posts( $query ) {
     
    if( ( $query->is_archive() || is_search ) && $query->is_main_query() && !is_admin() ) {
        
		$tax_query = $query->get('tax_query');
		$tax_query[] = array(
			'taxonomy' => 'book-author', // taxonomy name
			'field' => 'slug',
			'terms' => array( 'adam', 'adam2016' ), // term slug for exclude
			'operator' => 'NOT IN'
		);
		$tax_query['relation'] = 'AND';
		$query->set('tax_query', $tax_query);
    
    }
}

If it does not, please provide some example to test with:
- Example of an archive page and what posts should not appear on it.
- Example of search and what posts should not appear on it.

#2048897

Hi I tried this, I wasn't sure what I needed to enter under 'taxonomy'? It says 'taxonomy name' but it doesn't say if its plural or singular, or the slug (since it's hypthenated)? None worked either way, unfortunately.

Navigate to the product category dresses. The first one visible product is '’90s Multicolor Popart Dress'. It should not be visible (is early bird). In fact, I have radio filters on top of each category. Every category that includes the taxonomy term 'early birds' should not ve visible.

Doing a search for 'popart' will also display the '90s popart dress' that ought not be displayable. Hope that helps and thank you for your help!

#2049353

You need to use the Taxonomy slug in the query. The code that I shared has some issues, but even after fixing them, I still do not get the expected results. I need to debug this further in a minimal setup to exclude any possible conflict with other components.

Would you allow me to take a copy of your website and debug it locally?

#2049369

Hi, thank you for having a look. Yes, absolutely go ahead!

#2049373

I'm assuming that any changes done on the copy won't be pushed to live, but are merely taking place for debugging the issue? And that's it's safe for me to convert images to webp in the meantime without worry about having this reversed?

#2049405

Yes, indeed. I will use the copy locally to find out why the custom code is not giving the expected results. Once I get it fixed, we'll just replace the existing custom code on your website.

I'll keep you updated as soon as possible.

#2050813

It turns out to be an issue with the priority of the hook. The following code worked for me.

add_action( 'pre_get_posts', 'prefix_hide_term_posts', 9999, 1 );

function prefix_hide_term_posts( $query ) {
  
  if( ( is_archive() || is_search() ) && $query->is_main_query() && !is_admin() ) {
    
    $tax_query = $query->get('tax_query');
    $tax_query[] = array(
      'taxonomy' => 'garment-type',
      'field' => 'slug',
      'terms' => array( 'early-bird' ),
      'operator' => 'NOT IN'
    );
    $tax_query['relation'] = 'AND';

	$query->set('tax_query', $tax_query);
  }
}

It seems to work on your website too. The dresses archive does not return the "90s Multicolor Popart Dress" post, and the search does not return any results at all. Can you check again from your side?

#2050873

Amazing, I had a look and it works! I'm so happy to see this, thank you haha.

I've tested, finally, the original code on a view too, and it worked there too (the views-specific one, of course). The only question I have left is, if I were to want to apply the original, views-specific, code to multiple views, for the same taxonomy and taxonomy term, how would I best edit the code?

It currently looks like this, but I also want to apply it to a view with the ID 13707

 add_filter( 'wpv_filter_query', 'exclude_terms_func', 10, 3 );
function exclude_terms_func($query, $setting, $views_ID)
{
    if($views_ID == 11095) // your view id
    {
        $query['tax_query'][] = array(
            'taxonomy' => 'garment-type', // taxonomy name
            'field' => 'slug',
            'terms' => array( 'early-bird' ), // term slug for exclude
            'operator' => 'NOT IN'
        );
        $query['tax_query']['relation'] = 'AND';
    }
    return $query;
}

Is it just a matter of doing the same code below it, or is there a more elegant and efficient way of using the same code over 2 views?

This isn't a separate issue, different from this ticket; I thought that "archives" were functionally views, hence the wording of my original post. But I was looking to exclude it from my archive as well as other views.

But again, thank you for all your help so far, I truly appreciate it!

#2051451

You can adapt the code to be used in multiple views. Something like:

add_filter( 'wpv_filter_query', 'exclude_terms_func', 10, 3 );
function exclude_terms_func($query, $setting, $views_ID)
{
    $my_views = array(13707, 11095);
    if( in_array( $views_ID, $my_views ) ) // your view id
    {
        $query['tax_query'][] = array(
            'taxonomy' => 'garment-type', // taxonomy name
            'field' => 'slug',
            'terms' => array( 'early-bird' ), // term slug for exclude
            'operator' => 'NOT IN'
        );
        $query['tax_query']['relation'] = 'AND';
    }
    return $query;
}

Check lines 4-5 where we test against multiple views IDs.

#2051515

Everything is resolved. Consider this ticket closed, thank you so much for fixing this!

#2051749

Unfortunately, I just noticed something I didn't before: When clicking on one of the stylized radio filters at the top of the archive (in the dresses section, in this example). 'Early bird' no longer appears by default. But when I click on any of the other filters, it reappears.

So I would appreciate some further assistance!

#2052013

Sorry, I don't understand. What archive? The Dresses archive does not have a dropdown. Can you add some screenshot? Please include the URLs in the screenshots.

#2052455
click on one of these.png
early bird appears.png

Of course, no problem.

Not a dropdown, but radio filters, stylized. As seen in the screenshot, if I click on, for example, mini/midi/maxi, suddenly 'early bird' appears again. And when you click on early bird, the hidden product appears again.