I'm pretty new to wordpress. My custom query pagination not working properly, it arrives until page 17 than stops working (i have 21 pages of products).
I have tried to change it with pre get posts function but it doesn't work as expected. what could be the issue and what i can try?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$term = get_queried_object();
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'paged' => $paged,
);
if ($term instanceof WP_Term && 'product_cat' === $term->taxonomy) {
$taxonomy_slug = $term->slug;
// Custom query to get products from the current taxonomy.
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $taxonomy_slug,
),
),
);
}
$query = new WP_Query($args);
if (strpos($_SERVER['REQUEST_URI'], 'category') !== false) {
// Filter categories based on the visible products
$object_ids = $query->posts ? wp_list_pluck($query->posts, 'ID') : array();
} else {
// No filtering, include all products
$object_ids = array();
}
$product_categories = get_terms(array(
'taxonomy' => 'product_cat',
'hide_empty' => true, // Exclude empty categories
'object_ids' => $query->posts ? wp_list_pluck($query->posts, 'ID') : array(),
));
?>
<div class="pagination">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages
) ); ?>
</div>
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand