syner
syner

Reputation: 96

Exclude specific Woocommerce product category term(s) from a WP Query

I'm trying to load latest product in Woocommerce (on home page). I want to exclude specific product category from display on the loop, but exclude product category id is not working for me.

What I have done so far:

<?php  
    $args = array(
    'post_type'             => 'product',   
    'posts_per_page'        => 12,
    'post_status'           => 'publish',
    'taxonomy' => 'product_cat',
    'exclude'    => 29,//exclude mention category id from loop
    'parent'   => 0

    );

    $loop = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post();
        global $product;
?>

    <div class="popular-inner">
        <a href="<?php echo get_permalink(); ?>">

            <div class="popular-image d-flex ">
                <div class="align-self-center mx-auto ">
                    <?php the_post_thumbnail(); ?>
                </div>

            </div>

            <div class="popular-content-wp">
            <div class="popular-title">
                <h6><?php echo get_the_title(); ?></h6>
            </div>
            <div class="popular-price">
                <p><?php echo wc_price($product->get_price()); ?></p>
            </div>
            <div class="popular-add-to-cart">
                <ul>
                    <li>
                        <a href="<?php echo get_permalink(); ?>" class="dodo">Add to Cart</a>
                    </li>
                </ul>
            </div>
            </div>
        </a>    
    </div>

<?php endwhile; wp_reset_query();?>

Upvotes: 1

Views: 2733

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254378

Since wordpress version 3.1 using a taxonomy slug in a WP_Query is deprecated in flavor of tax_query. Also there are some other mistakes. See WP_Query taxonomy parameters.

Your revisited code:

<?php
    $loop = new WP_Query( array(
        'post_type'      => 'product',
        'posts_per_page' => 12,
        'post_status'    => 'publish',
        'parent'         => 0,
        'tax_query'      => array( array(
            'taxonomy' => 'product_cat',
            'field'    => 'term_id',
            'terms'    => array( 29 ), // Term ids to be excluded
            'operator' => 'NOT IN' // Excluding terms
        ) ),
    ) );

    if ( $loop->have_posts() ) :

    while ( $loop->have_posts() ) : $loop->the_post();

    // Get the instance of the WC_Product from the post ID
    $product = wc_get_product( get_the_id() );
?>
    <div class="popular-inner">
        <a href="<?php echo get_permalink(); ?>">

            <div class="popular-image d-flex ">
                <div class="align-self-center mx-auto ">
                    <?php the_post_thumbnail(); ?>
                </div>

            </div>

            <div class="popular-content-wp">
            <div class="popular-title">
                <h6><?php echo get_the_title(); ?></h6>
            </div>
            <div class="popular-price">
                <p><?php echo wc_price( $product->get_price() ); ?></p>
            </div>
            <div class="popular-add-to-cart">
                <ul>
                    <li>
                        <a href="<?php echo get_permalink(); ?>" class="dodo">Add to Cart</a>
                    </li>
                </ul>
            </div>
            </div>
        </a>
    </div>
<?php
    endwhile;
    wp_reset_postdata();

    else: ?>
    <p><?php _e( 'No posts found.' ); ?></p>

    <?php endif;
?>

It should work now.

Upvotes: 2

Related Questions