Skip to main content
added 114 characters in body
Source Link
user24742
user24742

I am trying to block the visualization of a custom post, of type 'product', if it is assigned to certain values of the taxonomy 'product_cat'.

Strictly speaking, I would like to block the visualization of a product if it falls under certain categories.

I can not make this work, even by hardcoding a forbidden name for a product_cat ('plus' in this example).

function filter($query){
   if ($query->is_single && is_main_query()) {
        $tax_query = array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'name',
                'terms' => array('plus'),
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
        return;
    }
}
add_filter('pre_get_posts','filter');

If I try to visualize a product falling under the 'plus' category, Wordpress shows it despite this filter.

What am I missing here?

Cheers!

Edit: This code goes in a Wordpress plugin, I forgot to mention this. I am not allowed to touch any template..

I am trying to block the visualization of a custom post, of type 'product', if it is assigned to certain values of the taxonomy 'product_cat'.

Strictly speaking, I would like to block the visualization of a product if it falls under certain categories.

I can not make this work, even by hardcoding a forbidden name for a product_cat ('plus' in this example).

function filter($query){
   if ($query->is_single && is_main_query()) {
        $tax_query = array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'name',
                'terms' => array('plus'),
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
        return;
    }
}
add_filter('pre_get_posts','filter');

If I try to visualize a product falling under the 'plus' category, Wordpress shows it despite this filter.

What am I missing here?

Cheers!

I am trying to block the visualization of a custom post, of type 'product', if it is assigned to certain values of the taxonomy 'product_cat'.

Strictly speaking, I would like to block the visualization of a product if it falls under certain categories.

I can not make this work, even by hardcoding a forbidden name for a product_cat ('plus' in this example).

function filter($query){
   if ($query->is_single && is_main_query()) {
        $tax_query = array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'name',
                'terms' => array('plus'),
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
        return;
    }
}
add_filter('pre_get_posts','filter');

If I try to visualize a product falling under the 'plus' category, Wordpress shows it despite this filter.

What am I missing here?

Cheers!

Edit: This code goes in a Wordpress plugin, I forgot to mention this. I am not allowed to touch any template..

typo
Source Link
user24742
user24742

I am trying to block the visualization of a custom post, of type 'product', if it is assigned to certain values of the taxonomy 'product_cat'.

Strictly speaking, I would like to block the visualization of a product if it falls under certain categories.

I can not make this work, even by hardcoding a forbidden name for a product_cat ('plus' in this example).

function filter($query){
   if ($query->is_single && is_main_query()) {
        $tax_query = array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'name',
                'terms' => array('plus'),
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
        return;
    }
}
add_filter('pre_get_posts','filter');

If I try to visualize a product falling under the 'plus' category, Wordpress shows it despite this filter.

What am I missing here?

Cheers!

I am trying to block the visualization of a custom post, of type 'product', if it is assigned to certain values of the taxonomy 'product_cat'.

Strictly speaking, I would like to block the visualization of a product if it falls under certain categories.

I can not make this work, even by hardcoding a forbidden name for a product_cat ('plus' in this example).

function filter($query){
   if ($query->is_single && is_main_query()) {
        $tax_query = array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'name',
                'terms' => array('plus'),
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
        return;
}
add_filter('pre_get_posts','filter');

If I try to visualize a product falling under the 'plus' category, Wordpress shows it despite this filter.

What am I missing here?

Cheers!

I am trying to block the visualization of a custom post, of type 'product', if it is assigned to certain values of the taxonomy 'product_cat'.

Strictly speaking, I would like to block the visualization of a product if it falls under certain categories.

I can not make this work, even by hardcoding a forbidden name for a product_cat ('plus' in this example).

function filter($query){
   if ($query->is_single && is_main_query()) {
        $tax_query = array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'name',
                'terms' => array('plus'),
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
        return;
    }
}
add_filter('pre_get_posts','filter');

If I try to visualize a product falling under the 'plus' category, Wordpress shows it despite this filter.

What am I missing here?

Cheers!

Source Link
user24742
user24742

Do not show a custom post if it belongs to a certain custom taxonomy

I am trying to block the visualization of a custom post, of type 'product', if it is assigned to certain values of the taxonomy 'product_cat'.

Strictly speaking, I would like to block the visualization of a product if it falls under certain categories.

I can not make this work, even by hardcoding a forbidden name for a product_cat ('plus' in this example).

function filter($query){
   if ($query->is_single && is_main_query()) {
        $tax_query = array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'name',
                'terms' => array('plus'),
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
        return;
}
add_filter('pre_get_posts','filter');

If I try to visualize a product falling under the 'plus' category, Wordpress shows it despite this filter.

What am I missing here?

Cheers!