69

I used the following code to get the list of product categories form WooCommerce in my WordPress website:

 <?php
  $taxonomy     = 'product_cat';
  $orderby      = 'name';  
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 0;      // 1 for yes, 0 for no  
  $title        = '';  
  $empty        = 0;
$args = array(
  'taxonomy'     => $taxonomy,
  'orderby'      => $orderby,
  'show_count'   => $show_count,
  'pad_counts'   => $pad_counts,
  'hierarchical' => $hierarchical,
  'title_li'     => $title,
  'hide_empty'   => $empty
);
?>
<?php $all_categories = get_categories( $args );

//print_r($all_categories);
foreach ($all_categories as $cat) {
    //print_r($cat);
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;

?>     

<?php       

        echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>'; ?>


        <?php
        $args2 = array(
          'taxonomy'     => $taxonomy,
          'child_of'     => 0,
          'parent'       => $category_id,
          'orderby'      => $orderby,
          'show_count'   => $show_count,
          'pad_counts'   => $pad_counts,
          'hierarchical' => $hierarchical,
          'title_li'     => $title,
          'hide_empty'   => $empty
        );
        $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
                echo  $sub_category->name ;
            }

        } ?>



    <?php }     
}
?>

This works fine and returns the list of product categories. I have been trying now to get a list of products for a particular category.

Example: get all the products for with cat_id=34.

I know products are stored as posts, and have been trying to get this done but can't seem to.

How do I get the list of products for a particular category?

4 Answers 4

111
<?php  
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => 10,
        'product_cat'    => 'hoodies'
    );

    $loop = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post();
        global $product;
        echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
    endwhile;

    wp_reset_query();
?>

This will list all product thumbnails and names along with their links to product page. change the category name and posts_per_page as per your requirement.

5
  • 2
    Use 'product_cat' => $sub_category->slug if you want to use this in the nested inner loop in your code. Commented Jan 9, 2014 at 19:46
  • 2
    Some of your guys out there are just genius. There should be a way of rewarding extremely helpful comment/people :)
    – Shina
    Commented Feb 4, 2015 at 18:25
  • I want to use get_posts( arg). How I can do it ?
    – huykon225
    Commented Mar 22, 2017 at 1:46
  • 1
    This way you get post objects, if i wanted to get product objects what should i use? Commented May 10, 2018 at 10:14
  • Thank you. Exactly what I was looking for.
    – Flagmans
    Commented Nov 29, 2021 at 19:02
89

Do not use WP_Query() or get_posts(). From the WooCommerce doc:

wc_get_products and WC_Product_Query provide a standard way of retrieving products that is safe to use and will not break due to database changes in future WooCommerce versions. Building custom WP_Queries or database queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance.

You can retrieve the products you want like this:

$args = array(
    'category' => array( 'hoodies' ),
    'orderby'  => 'name',
);
$products = wc_get_products( $args );

WooCommerce documentation

Note: the category argument takes an array of slugs, not IDs.

2
  • 2
    This doesn't work with ID of category. It expects the category slug.
    – Himadri
    Commented Jun 23, 2020 at 13:56
  • @Himadri You seem to be right. The documentation only mentions slugs. I will amend the example. Commented Jun 23, 2020 at 19:42
26
<?php
$args     = array( 'post_type' => 'product', 'category' => 34, 'posts_per_page' => -1 );
$products = get_posts( $args ); 
?>

This should grab all the products you want, I may have the post type wrong though I can't quite remember what woo-commerce uses for the post type. It will return an array of products

3
  • I have tried your snippet but it return only 5 products although I tried post_per_page = -1. can you tell me why ?
    – huykon225
    Commented Mar 22, 2017 at 1:48
  • 1
    @huykon225 The correct parameter would be posts_per_page, not post_per_page.
    – MrHunter
    Commented Mar 23, 2017 at 12:25
  • @huykon225 No problem!
    – MrHunter
    Commented Mar 23, 2017 at 15:28
0

Always use tax_query to get posts/products from a particular category or any other taxonomy. You can also get the products using ID/slug of particular taxonomy...

$the_query = new WP_Query( array(
    'post_type' => 'product',
    'tax_query' => array(
        array (
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'accessories',
        )
    ),
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    the_title(); echo "<br>";
endwhile;


wp_reset_postdata();
1

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.