0

i have product cat with id=5 and have 10 product

i show list 5 product by id as

$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 5 );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); global $product;

**** loop code *****

endwhile;
wp_reset_query();

but is error code(don't show any product). if i change 'product_cat' => 5 by slug as 'product_cat' => 'slug_name' is true.

How to show product by id = 5

Thanks

1 Answer 1

2

You should use "tax query" like this:

$args = array( 
    'post_type' => 'product', 
    'posts_per_page' => 5,
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'terms'    => 5,
        ),
    ),
);

Using taxonomy name (product_cat) as WP_Query parameter is deprecated.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.