Reputation: 79
I'm using this code for prodcut loop on custom shop page:
<?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
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 http://test.art-electrik.ru/wrap/dark/wordpress/shop/
Upvotes: 3
Views: 3077
Reputation: 76
Since you do have a custom loop there's no need to use the pre_get_posts filter. Just add the code you want to your WP_Query parameters. If you still want to use the filter, you'll need to use the parse_tax_query filter instead. As the pre_get_posts is fired too late to make a proper tax query adjustments. So your parameters, for your custom query, will look like this:
$params = array(
'posts_per_page' => 12,
'post_type' =>'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'free' ),
'operator' => 'NOT IN'
)
)
);
$wc_query = new WP_Query( $params );
Upvotes: 2
Reputation: 254373
You should try to use in your loop, the WordPress conditional function has_term()
.
So your code will be:
<?php
// Set here the category id, slug or name to be removed
$removed_category = 'my_category';
$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(); ?>
<?php if ( !has_term( $removed_category, 'product_cat' ) ): // <== <== <== ## HERE ## ?>
<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 endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php _e( 'No Products'); ?>
</p>
<?php endif; ?>
This should work.
Upvotes: 1