Use 1 of these 7 PHP code snippets at the end of your child themes functions file to set products per page on WooCommerce archive page types including your shop, product category and product tag archive pages :
add_filter( 'loop_shop_per_page', 'products_per_page_category', 20 ); function products_per_page_category( $count ) { if ( is_product_category() ) { return -1; // Display all products } return $count; // For other pages, keep the default number of products per page }
You can also use the woocommerce_product_query
action hook to do exactly the same thing :
add_action( 'woocommerce_product_query', 'display_all_products_on_category_pages' ); function display_all_products_on_category_pages( $query ) { if ( ! is_admin() && $query->is_main_query() && is_product_category() ) { $query->set( 'posts_per_page', -1 ); } }
You can also use pre_get_posts
to get the same result :
add_action( 'pre_get_posts', 'display_all_products_on_category_pages' );
function display_all_products_on_category_pages( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_product_category() ) {
$query->set( 'posts_per_page', -1 );
}
}
Specific Taxonomy
To target a specific category, you would include a conditional tag like this :
add_action( 'woocommerce_product_query', 'display_all_products_on_category_pages' );
function display_all_products_on_category_pages( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_product_category('news') ) {
$query->set( 'posts_per_page', -1 );
}
}
The above snippet targets the product category archive named news.
To target a custom taxonomy term archive, use code like this
add_action( 'woocommerce_product_query', 'display_all_products_on_category_pages' );
function display_all_products_on_category_pages( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_tax('taxonomy', 'term') ) {
$query->set( 'posts_per_page', -1 );
}
}
In the above snippet, swap out taxonomy with the name of your taxonomy and term with the name of your term.
Sub Categories
To target all sub or child category archives, use code like this :
add_filter('loop_shop_per_page', 'modify_shop_per_page');
function modify_shop_per_page($products_per_page) {
if ( is_shop() || is_product_category() || is_product_tag() ) {
$parent_cat_slug = 'category-one';
$parent_cat = get_term_by('slug', $parent_cat_slug, 'product_cat');
if ( $parent_cat ) {
$child_cats = get_terms(array(
'taxonomy' => 'product_cat',
'child_of' => $parent_cat->term_id,
'fields' => 'ids'
));
if ( ! empty( $child_cats ) ) {
return 2; // Set the number of products per page for child categories
}
}
}
return $products_per_page;
}
Swap out the value for $parent_category_slug with your parent category slug.
To target a specific sub category, use code like this :
add_filter('loop_shop_per_page', 'target_sub_category_archive');
function target_sub_category_archive( $products_per_page ) {
if ( is_product_category() ) {
$specific_sub_cat_slug = 'child-of-category-one'; // Replace with your specific subcategory slug
$queried_object = get_queried_object();
if ($queried_object && $queried_object->slug === $specific_sub_cat_slug) {
return 1; // Set the number of products per page for the specific subcategory
}
}
return $products_per_page;
}
Make sure to swap out child-of-category-one in the above code with the slug of your child category.
In WooCommerce, you can use any of these hooks :
- woocommerce_product_query
- loop_shop_per_page
- pre_get_posts
Was This Tutorial Helpful?