Hi, I’m having a problem adding pagination in ACF relationship. I’m not sure what’s wrong but it’s already showing and the path is correct on the link but it just reload the page and nothing happens.
here is my code. this is originally added inside a shortcode to be added in elementor template.
Thank you.
<?php $related_products = get_field( 'related_products' );
if ( $related_products ) {
global $paged;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'fields' => 'ids',
'post__in' => $related_products,
'posts_per_page' => 4,
'paged' => $paged,
);
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
?><?php //pagination addition
$total_pages = $custom_query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
} //end pagination ?>
<?php
else :
// do something else
echo "Sorry no slected products right now!";
endif;
wp_reset_postdata();
}
Is this returning anything other than 1?
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
I suspect that it’s not getting set at all because there is not a rewrite rule that covers a url like
/product/$PRODUCT_NAME/page/XX
To get the paged query var populated you will likely need to add a rewrite rule for it.