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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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