Fixing WooCommerce Pagination Issues in Deep Subcategories
Super excited to share this fix with you!
If you’ve encountered 404 errors when navigating deep subcategories in WooCommerce product archives, you’re not alone. This issue occurs when WooCommerce misinterprets query parameters or struggles with category hierarchies.
Problem Breakdown
- Top-level categories → Pagination works fine.
- First subcategory level → Still works.
- Second subcategory level → Clicking “Page 2” leads to a 404 Not Found error.
If this sounds familiar, here’s a simple function and JavaScript snippet to fix it.
Step-by-Step Fix
1. Modify WooCommerce Query to Handle Deep Subcategories
I use WPCode Lite to add functions.
Add the following PHP function to ensure WooCommerce correctly processes pagination within sub-subcategories:
function fix_subcategory_pagination($query) {
if (!is_admin() && $query->is_main_query() && is_product_category()) {
$paged = get_query_var('paged') ? absint(get_query_var('paged')) : 1;
$query->set('paged', $paged);
$query->set('posts_per_page', 12); // Adjust as needed
if ($query->is_archive) {
$query->set('tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => get_query_var('product_cat'),
'include_children' => true,
),
));
}
}
}
add_action('pre_get_posts', 'fix_subcategory_pagination');
What This Does:
- Ensures WooCommerce recognizes pagination requests within sub-subcategories.
- Applies the fix only to product category pages.
- Correctly processes deeper category parameters for smoother navigation.
2. Add JavaScript to Improve Loading Behavior
Insert the following JavaScript code in Code Snippets:
jQuery(document).ready(function($) {
var page = 2;
var loading = false;
$(window).scroll(function() {
if ($(window).scrollTop() + $(window).height() >= $(document).height() - 200 && !loading) {
loading = true;
$.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'POST',
data: {
action: 'load_more_products',
page: page
},
success: function(response) {
$('.products').append(response);
page++;
loading = false;
}
});
}
});
});
What This Does:
- Improves product loading when navigating deep subcategories.
- Reduces pagination-related errors without requiring page reloads.
3. Flush Permalinks
After adding the PHP function:
- Go to WordPress Dashboard → Settings → Permalinks
- Click Save Changes (without modifying anything)
- Test pagination for deeper subcategories
4. Test Pagination Behavior
Try visiting: https://yourdomain.com/product-category/your-sub-category/page/2/
If the page loads properly without a 404 error, the fix worked!
Final Thoughts
This method ensures WooCommerce correctly handles pagination in sub-subcategories, eliminating frustrating errors when navigating deep product listings.