I'm trying to create a custom theme. I added theme compatibility with Woocommerce to my theme, and am trying to create a product loop. I want to filter my loop so it only displays post with the category "cap" I tried the following versions:

$params = array('posts_per_page' => 5, 'post_type' => 'product', 'category_name' => 'cap');

$wc_query = new WP_Query($params); ?>

and also tried displaying it by Category ID. Both show me no products. Am I doing something wrong? Using PHP 7.2

Some images showing cat setup:



Any help is appreciated!


woocommerce categories are not a "category" they're the product_cat taxonomy. change your args like so:

$params = array(
    'posts_per_page' => 5,
    'post_type' => 'product',
    'tax_query' => array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'cap'

If you use WP_Query then according to the WooCommerce wiki:

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.

The best way to create a custom loop is to use wc_get_products() or WC_Product_Query.

In your case, I would do something like this:

$paged                   = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
$ordering                = WC()->query->get_catalog_ordering_args();
$ordering['orderby']     = array_shift(explode(' ', $ordering['orderby']));
$ordering['orderby']     = stristr($ordering['orderby'], 'price') ? 'meta_value_num' : $ordering['orderby'];
$products_per_page       = 5;// default is apply_filters('loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page());

$category_products       = wc_get_products(array(
  'status'               => 'publish',
  'limit'                => $products_per_page,
  'page'                 => $paged,
  'paginate'             => true,
  'return'               => 'ids',
  'orderby'              => $ordering['orderby'],
  'order'                => $ordering['order'],
  'category'             => array('cap')

wc_set_loop_prop('current_page', $paged);
wc_set_loop_prop('is_paginated', wc_string_to_bool(true));
wc_set_loop_prop('page_template', get_page_template_slug());
wc_set_loop_prop('per_page', $products_per_page);
wc_set_loop_prop('total', $category_products->total);
wc_set_loop_prop('total_pages', $category_products->max_num_pages);

if($category_products) {
    foreach($category_products->products as $featured_product) {
      $post_object = get_post($featured_product);
      setup_postdata($GLOBALS['post'] =& $post_object);
      wc_get_template_part('content', 'product');
} else {

I actually wrote a post about this with more details here if you're curious or want to extend this further: https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/

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.