Set different “products per page” number for specified WooCommerce Category

You might want to get different product per pages for each category.

For example for Phones category, you want to display 10 phones per page, for Furniture, you want only 4 products per page, for Accessories, you want to display 100 products per page.

To achieve that is easy, here is how:

add_action('pre_get_posts',function($query){
    if(!empty($query->query["product_cat"]) 
        and !empty($query->queried_object->term_id)){

        //product category with term_id = 199
        if($query->queried_object->term_id == 199){
            $query->set("posts_per_page",20);
        }

        //product category with term_id = 200
        if($query->queried_object->term_id == 200){
            $query->set("posts_per_page",4);
        }
  
    }
    return $query;
});

You can create any amount of small if() block for product categories.

And it is obvious that you can do the same with any kind of WP Taxonomy, not only for Product category.

For example if you want to do the same with WP Category, then just replace “product_cat” with “category” in the given code above.

Get more useful WP tricks and snippets by subscribing to my mailclub.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.