I am building WooCommerce store with storefront child theme. In my shop page (archive-product.php
) I’m using following code to display products:
<?php
$filterArray = explode("/", "http://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI]);
if(count($filterArray)>5) {
echo do_shortcode('[products category="' . $filterArray[count($filterArray)-2] . '" per_page="30" limit="30" columns="3" paginate="true"]');
}
else {
echo do_shortcode('[products per_page="30" limit="30" columns="3" paginate=true]');
}
?>
When I don’t pass category
attribute to the shortcode, everything works as expected. But when I add category
, my page display not 30, but only 2 products per page. Changing products
to product category
does not work at all. What could be the reason of such strange behaviour?
EDIT: I have some mess in my code, found following function in functions.php
:
function modify_product_cat_query( $query ) {
if (!is_admin() && $query->is_tax("product_cat")){
$query->set('posts_per_page', 2);
}
}
add_action( 'pre_get_posts', 'modify_product_cat_query' );
I thought that I finally found the issue, but even when I delete that sinppet or change value from 2 to 30, category page still displays only two products…
2
Answers
You can use
woocommerce_product_query
to changeposts_per_page
. check the below code.Try using this. You are on archive page so it is more easy to get current term. You don’t have to fetch it from url.
The issue you are facing product are not displaying more than 2 it’s probably because there is something overriding posts_per_page.