TransWikia.com

Pagination is not working on custom query inside a custom home page template

WordPress Development Asked by Naresh Devineni on February 20, 2021

I know this is a duplicate question. But to comment on existing question, I dont have enough points.

So here we go….

Pagination is not working on custom query inside a custom home page template.

The solution (which worked for many others) below is taken from another question.

How to fix pagination for custom loops?


<div class="most-popular type-1">
   <h3 class="section-headline"><?php _e('Most Popular','themename'); ?></h3>
   <div class="widget-articles clearfix">
      <?php 
         $mostpopular_args=array(
             'post_type'        => 'post',
             'orderby'          => 'meta_value_num',
             'meta_key'         => 'view_count',
             'posts_per_page'   => '10',
         );
         $mostpopular_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
         ?>
      <?php 
         $mostpopular_pick = new WP_Query($mostpopular_args); 
         // Pagination fix
         $temp_query = $wp_query;
         $wp_query   = NULL;
         $wp_query   = $mostpopular_pick;
         ?>
      <?php if($mostpopular_pick->have_posts() ): ?>
      <?php while($mostpopular_pick->have_posts()): $mostpopular_pick->the_post(); ?>
      <article class="widget-article">
         <a href ="<?php the_permalink(); ?>">
            <div class="widget-article-thumb">
               <?php if(has_post_thumbnail()){ ?>
               <?php $page_thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'featuredsmall'); ?>
               <img src="<?php echo $page_thumb[0]; ?>" alt="" />   
               <?php } ?>
            </div>
         </a>
         <div class="widget-content-wrapper">
            <h3 class="widget-article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
         </div>
         <div class="only-count">
            <div class="icon icon-share-alt" aria-hidden="true"></div>
            <?php echo do_shortcode('[mashshare shares="true" buttons="false"]'); ?>
            <div class="share-text">SHARES</div>
         </div>
      </article>
      <?php endwhile; ?>
      <?php endif; ?>
      <?php 
         wp_reset_postdata(); 
         // Custom query loop pagination
         previous_posts_link( 'Older Posts' );
         next_posts_link( 'Newer Posts', $mostpopular_pick->max_num_pages );
         // Reset main query object
         $wp_query = NULL;
         $wp_query = $temp_query;
      ?>
   </div>
</div>

However this is not working for me, I dont know why. Is there any external factor?

Between this is my total custom home page template.


<?php
/*
Template Name: Home Page
*/
?>
<?php get_header(); ?>
    <div class="main-content col-xs-12" role="main">
        <div class="row">
            <?php get_sidebar('abovethefold'); ?>
        </div>
        <div class="row">
            <?php get_sidebar('homecolumnone'); ?>
            <div class="homecolumntwo-sidebar col-sm-4 col-md-5">
                <div class="most-popular type-1">
                    <h3 class="section-headline"><?php _e('Most Popular','themename'); ?></h3>
                    <div class="widget-articles clearfix">
                            <?php 
                                $mostpopular_args=array(
                                    'post_type'         => 'post',
                                    'orderby'           => 'meta_value_num',
                                    'meta_key'          => 'view_count',
                                    'posts_per_page'    => '10',
                                );
                                $mostpopular_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
                            ?>
                            <?php 
                                $mostpopular_pick = new WP_Query($mostpopular_args); 
                                // Pagination fix
                                $temp_query = $wp_query;
                                $wp_query   = NULL;
                                $wp_query   = $mostpopular_pick;
                            ?>

                            <?php if($mostpopular_pick->have_posts() ): ?>
                                <?php while($mostpopular_pick->have_posts()): $mostpopular_pick->the_post(); ?>
                                    <article class="widget-article">
                                        <a href ="<?php the_permalink(); ?>">
                                            <div class="widget-article-thumb">
                                                <?php if(has_post_thumbnail()){ ?>
                                            <?php $page_thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'featuredsmall'); ?>
                                                    <img src="<?php echo $page_thumb[0]; ?>" alt="" />  
                                                <?php } ?>
                                            </div>
                                        </a>
                                            <div class="widget-content-wrapper">
                                                <h3 class="widget-article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                            </div>
                                            <div class="only-count">
                                                <div class="icon icon-share-alt" aria-hidden="true"></div>
                                                <?php echo do_shortcode('[mashshare shares="true" buttons="false"]'); ?>
                                                <div class="share-text">SHARES</div>
                                            </div>
                                    </article>
                                <?php endwhile; ?>
                            <?php endif; ?>
                            <?php 
                                wp_reset_postdata(); 
                                // Custom query loop pagination
                                previous_posts_link( 'Older Posts' );
                                next_posts_link( 'Newer Posts', $mostpopular_pick->max_num_pages );
                                // Reset main query object
                                $wp_query = NULL;
                                $wp_query = $temp_query;
                            ?>
                    </div>
                </div>
            </div> <!-- end sidebar -->
            <?php //get_sidebar('homecolumntwo'); ?>
            <?php get_sidebar('homecolumnthree'); ?>
        </div>
    </div> <!-- end main-content -->
<?php get_footer(); ?>

Any help would be highly appreciated 🙂 Thank You <3

One Answer

Use this for pagination

<?php
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

    $mostpopular_args=array(
             'post_type' => 'post',
             'orderby' => 'meta_value_num',
             'meta_key' => 'view_count',
             'paged' => $paged,
             'posts_per_page' => '10',
         );
    $wp_query = new WP_Query($mostpopular_args);

    while ($wp_query->have_posts()): $wp_query->the_post();
    get_the_title();
    endwhile;

    global $wp_query;

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

Answered by jay.jivani on February 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP