Skip Navigation

[Resolved] Show results in all taxonomies EXCEPT one

This support ticket is created 5 years, 8 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: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by Waqar 5 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#1222394

Tell us what you are trying to do?
When i first enter a page, i want the view (which has a search) to filter post by all taxonomies except one.

I have taxonomies which is years, the last one is called archive. So the select filter has this values:

2010
2011
2012
archive

I want this options to be always available BUT the first time the user visits the page i want that the posts inserted into the archive taxonomy are not visible unless the user selects it from the search.

Is this possible? and if so.. how?

thanks

#1222978

Hi Pedro,

Thank you for contacting us and I'll be happy to assist.

To exclude posts from a specific taxonomy term when the page with view loads, you can use filter "wpv_filter_query":
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example, you can add the following code in your active theme's "functions.php" file:


add_filter( 'wpv_filter_query', 'filter_exclude_cat_fn', 1000 , 3 );
function filter_exclude_cat_fn( $query_args, $view_settings ) {

	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 123) ) {

		if(empty($query_args['tax_query']))
		{
			$query_args['tax_query'][0]['taxonomy'] = 'taxonomy-slug';
			$query_args['tax_query'][0]['field'] = 'id';
			$query_args['tax_query'][0]['terms'][0] = 111;
			$query_args['tax_query'][0]['operator'] = 'NOT IN';
			$query_args['tax_query'][0]['include_children'] = '';

			$query_args['tax_query']['relation'] = 'AND';
		}
	
	}

	return $query_args;
}

Note: You'll replace:

- '123' with the actual view's ID.
- 'taxonomy-slug' with the actual slug of your taxonomy.
- '111' with the taxonomy term ID for the term to exclude which is "archive".

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1223021

Hi Waqar,

First of all, thank you for your time.
When i implement your code, the taxonomy dropdown appears empty and the views shows "no results found".

Actually if i remove some $query_args lines, the result is the same. Only when i remove the array number ([0]) from the code it shows the view correctly but with the ARCHIVE taxonomy also listed (which is wrong).

This is the actual code.

function filter_exclude_cat_fn( $query_args, $view_settings ) {
    if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 5281) ) {
        if(empty($query_args['tax_query']))
        {
            $query_args['tax_query'][0]['taxonomy'] = 'arquivo';
            $query_args['tax_query'][0]['field'] = 'id';
            $query_args['tax_query'][0]['terms'][0] = 123;
            $query_args['tax_query'][0]['operator'] = 'NOT IN';
            $query_args['tax_query'][0]['include_children'] = '';
 
            $query_args['tax_query']['relation'] = 'AND';
        }
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'filter_exclude_cat_fn', 1000 , 3 ); 

My question is.. perhaps there is some issue to the fact it's an array? When i check WordPress Codex regarding wp-query (code below) and taxonomies they place the tax_query inside an array, would it be that the case?

$args = array(
	'post_type' => 'post',
	'tax_query' => array(
		'relation' => 'AND',
		array(
			'taxonomy' => 'movie_genre',
			'field'    => 'slug',
			'terms'    => array( 'action', 'comedy' ),
		),
		array(
			'taxonomy' => 'actor',
			'field'    => 'term_id',
			'terms'    => array( 103, 115, 206 ),
			'operator' => 'NOT IN',
		),
	),
);
$query = new WP_Query( $args );

Sorry, im no php coder.

thank you once again.

#1223035

Hi Pedro,

Thanks for writing back and sharing the update.

It is strange that this code worked in my tests, but not on your website. But it could be due to some difference in the setup.

Can you please share temporary admin login details, so that I can check how the view, taxonomy and the search filter is configured?

I've set your next reply as private and though I'll not be making any changes, please make a complete backup copy of the website, before sharing the access details.

regards,
Waqar

#1223844

Hi Pedro,

Thank you for sharing the access details.

I noticed the slug of the "Anos de Publicação" custom taxonomy is "ano-de-publicacao", but in the code, the slug of one of its terms "Arquivo" is used (i.e. "arquivo").
( screenshot: hidden link )

Please replace "arquivo" with "ano-de-publicacao" and then check the view's results again.

regards,
Waqar