Remove category from woocommerce loop on custom shop page

admin

Administrator
Staff member
I'm using this code for prodcut loop on custom shop page:

Code:
 <?php
                $product = new WC_Product(get_the_ID());
                $params = array('posts_per_page' => 12, 'post_type' =>'product');
                $wc_query = new WP_Query($params);
                ?>
                <?php if ($wc_query->have_posts()) : ?>
                <?php while ($wc_query->have_posts()) :
                $wc_query->the_post(); ?>
                <article class="portfolio__item portfolio__item--shop">
                    <figure class="blog__image-container">
                        <?php if ( has_post_thumbnail()) {the_post_thumbnail('thumb-front' ,array("class"=>"portfolio__image post_thumbnail"));} ?>
                    </figure>
                    <h3 class="portfolio__content-title portfolio__content-title--shop"><?php the_title(); ?></h3>
                    <p class="portfolio__content-text portfolio__content-text--shop"><?php $product = new WC_Product(get_the_ID()); echo $product->get_price_html(); ?></p>
                    <a href="?add-to-cart=<?php echo $product->id; ?>" class="portfolio__link">
                        <div class="portfolio__content">
                            <p class="portfolio__content-text">Click to buy</p>
                        </div>
                    </a>
                </article>
                <?php endwhile; ?>
                <?php wp_reset_postdata(); ?>
    <?php else:  ?>
    <p>
     <?php _e( 'No Products'); ?>
     </p>
     <?php endif; ?>

And i want exclude one category from this loop. I'm trying this code

Code:
add_action( 'pre_get_posts', 'remove_cat_from_shop_loop' );


function remove_cat_from_shop_loop( $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( 'free' ), // Change it to the slug you want to hide
        'operator' => 'NOT IN'
    )));

}

remove_action( 'pre_get_posts', 'remove_cat_from_shop_loop' );


}

But it doesn't work for me. And one more moment. When i'm trying too add new category in admin panel i have undefined error, but after refreshing page new category exist. My page <a href="http://test.art-electrik.ru/wrap/dark/wordpress/shop/" rel="nofollow">http://test.art-electrik.ru/wrap/dark/wordpress/shop/</a>