Custom WP_Query pagination not working in all categories of template

I have a custom WP_Query in the product category template of Woocommerce, the problem is that the pagination only works in certain categories (i have no idea why) and if i change the main query with pre_get_posts, if i doesn’t change it, the pagination doesn’t works at all.

In some categories when i click to paginate it just displays a page with my Header and Footer. What could it be? I’ll be grateful if someone helps me.

$paged = ( get_query_var( ‘paged’ ) ) ? absint( get_query_var( ‘paged’ ) ) :
1;

$args = array(
‘posts_per_page’ => 1,
‘post_type’ => ‘product’,
‘paged’ => $paged
);

$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
while( $custom_query->have_posts() ) : $custom_query->the_post();

get_template_part(“templates/product-content-category”);

endwhile;
wp_reset_postdata();
}else{
echo ‘no posts’;
}

$big = 999999999;
echo paginate_links( array(
‘base’ => str_replace( $big, ‘%#%’, esc_url( get_pagenum_link( $big ) ) ),
‘format’ => ‘?paged=%#%’,
‘current’ => max( 1, (get_query_var(‘paged’)) ),
‘total’ => $custom_query->max_num_pages
) );

function customize_main_query( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;

if ( is_product_category() ) {
$query->set( ‘posts_per_page’, -1 );
return;
}
}
add_action( ‘pre_get_posts’, ‘customize_main_query’ );

Read more here:: Custom WP_Query pagination not working in all categories of template

Leave a Reply

Your email address will not be published. Required fields are marked *