Show Products by Category ID in WooCommerce

Quick Summary
- Editors pick the product category through an ACF field named category_id, and a tax_query on product_cat lists every product in it.
- Find the ID after tag_ID= in the address bar when editing the category under Products > Categories, then enter it in the field.
- With WooCommerce and ACF active, add the code to the page template and check that $cat_id has a value before the query.
This snippet lists all WooCommerce products from one product category, with the category ID taken from an Advanced Custom Fields (ACF) field on the current page. That way an editor can choose which category a page shows without touching the template. It needs both WooCommerce and ACF.
Put the code in the page template that should display the products.
<?php
$cat_id = get_field('category_id'); //USING ACF
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => - 1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $cat_id,
'operator' => 'IN'
// Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$loop = new WP_Query($args);
?>
<?php
if ($loop->have_posts()): ?>
<?php
while ($loop->have_posts()):
$loop->the_post(); ?>
<!-- Item Start -->
<div class="single-submission-item product-listing-carousel-item">
<div class="submission-item-action-btn">
<a href="<?php
the_permalink(); ?>" class="submission-item-view-details-btn"><?php
the_title(); ?></a>
</div>
</div>
<!-- Item End -->
<?php
endwhile;
endif; ?>
<?php
wp_reset_postdata(); ?>How it works
get_field( 'category_id' )reads the ID from an ACF field namedcategory_idon the current page. A Number field works, or a Taxonomy field set to return the term ID.- The
tax_queryselects products in thatproduct_catterm. With theINoperator you can also pass an array of IDs to show products from several categories; the comment in the code lists the other operators. 'posts_per_page' => -1loads every matching product. Set a number instead if the category is large.- Each product is printed as a link with its title. Replace the
divmarkup and CSS classes with your own layout, for example to add the price or image, or load WooCommerce’s own product card as in the featured products loop. wp_reset_postdata()restores the page’s own post data after the loop, so ACF fields and template tags further down still refer to the page.
Finding the category ID
Go to Products > Categories and click the category to edit it. The number after tag_ID= in your browser’s address bar is its ID. Enter that number in the page’s ACF field.
Pages that use this template without a category in the field won’t have anything to filter by, so it’s a good idea to check that $cat_id has a value before you run the query.
Related articles
- querying custom post types by taxonomy term ID: the same tax_query for other post types
- why ACF is still my choice for custom WordPress sites: the approach behind editor-controlled fields like this one
