My integrated woocommerce shop does not display the right items

Hello dear staff,

Since my manual update of Theme X a week ago (Meaning I had to delete files and reinstall files and any changes in code were lost), my shop is not displaying the right products.
Before, I was only showing certain category products in my shop and the rest was not accessible to the general public, from my shop page or menu.

I have 4 categories for products and some of the categories (“other”) were hidden on the shop page.
But suddenly they were also visible. So I now had to hide the shop page because else people could see all kinds of products that were not meant to be on the shop page and it got really confusing.

Now, I sort of remember that we changed the code (at the start of using X a few years ago), but I cannot find the thread since that was on the old forum.

Please help?

Hi There,

Thanks for writing in! Since we have changed our forum over the past few years, it will be not possible to locate your exact thread. The easiest way would be to contact your hosting provider and restore a previous backup.

Thanks!

Ha dear staff thank you, but i do not want to restore my previous back up. it’s impossible. too old etc.
My question is:

what do i need to change in the code to have some products displayed in the shop and others “hidden”?

My category OTHER i want to hide. So where and how can I change this in the code?
(Just like I did in my previous versions of the website. Please read again my originial question too.)

Please let me know and I will change it.

thank you.

Hi There,

Please try with this code:

function x_pre_get_posts( $query ) {
    if ( !is_admin() && is_shop() ) {
        $tax_query = array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => 'other',
                'operator' => 'NOT IN',
            ),
        );
        $query->set( 'tax_query', $tax_query );
  }
}
add_action( 'pre_get_posts', 'x_pre_get_posts' );

Hope it helps :slight_smile:

Ooowaaah! Thnx! We are not there but we are getting somewhere!
It doesn’t fully work
I found also my old code thanks to your advice.
Your code alone did not work but then I compared them and made a combination and this works.

function x_pre_get_posts( $query ) {
if ( !is_admin() && is_shop() ) {
$tax_query = array(
array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => array( ‘o’ ),
‘operator’ => ‘NOT IN’,
),
);
$query->set( ‘tax_query’, $tax_query );
}

I changed the parameter for terms:

INSTEAD OF
‘terms’ => ‘other’,

I TURNED IT INTO
‘terms’ => array( ‘o’ ),

It works. I hope I didn’t do something wrong. I can read it a little but I’m not that good with the code that I can 100% read it. Please let me know. I know I should not name any other category with an o.

This is the former code, in case you want to check…

Thanks so much!

// START of Don’t display products in the ‘other’ category on the shop page

function custom_pre_get_posts_query( $q ) {

if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;

if ( ! is_admin() && is_shop() ) {

$q->set( ‘tax_query’, array(array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => array( ‘o’ ), // Don’t display products in the ‘Other’ category on the shop page
‘operator’ => ‘NOT IN’
)));

}

remove_action( ‘pre_get_posts’, ‘custom_pre_get_posts_query’ );

}

Hey There,

We are just glad that you have figured it out a way to correct the said issue.
Thanks for letting us know!

Best Regards.

1 Like

You are welcome :slight_smile: Thank YOU!!! :slight_smile: