Pagination not working on custom query on a page

The question:

I have a custom query on a page (it is not an archive page), it works just fine, but I want pagination and it is not working at all…

                        <?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                                if(have_posts() ):
   $args = array( 'post_type' => 'store',
                  //    'posts_per_page' => 9,
                   'paged' => $paged,
                  'meta_key' => 'store_client_type',
                  'tax_query' => array(
                        array(
                            'taxonomy' => 'style',
                            'field'    => 'slug',
                            'terms'    => $style,
                        )
                    ),
                 'meta_query' => array(
                        'relation' => 'AND',
                        array(
                        'plan_clause' => array(
                                        'key'  => 'store_client_type_plan',
                                        'compare'   => 'EXISTS',
                                        ),
                                    ),
                        array(
                        'relation' => 'AND',
            )
                                ),
                  'orderby' => array(
                                'plan_clause'  => 'desc',
                            )   
                 ); 
    $loop = new WP_Query( $args );
        while($loop->have_posts()):  $loop->the_post(); ?>
                            
                                <div class="col-lg-4 col-md-6 col-sm-12 center">
                                    <div class="blog-article-card pt-2">
                                        <h4><?php the_title(); ?></h4>
                                        </div>
                                    </div>
                                </div>
                                <?php     
        endwhile;  
        endif;
                the_posts_pagination(); 
        wp_reset_query();   
                    ?> 

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

SOLVED
with the magic of paginate_links (after the endwhile)

<?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' => $loop->max_num_pages
) ); ?>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment