0

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

  • Is this a static front page – Pieter Goosen Dec 15 '14 at 10:10
  • 1
    If so, $mostpopular_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; should be $mostpopular_args['paged'] = get_query_var( 'page' ) ? get_query_var( 'page' ) : 1; – Pieter Goosen Dec 15 '14 at 10:16
  • Oh My God :D..... Thank you soo much...It worked like a charm <3... Can you please post this comment in the original question too? It would be a great help for people like me :) – Naresh Devineni Dec 15 '14 at 12:25
  • I mean right below where you left this comment "Wish pages in the codex could be so complete." – Naresh Devineni Dec 15 '14 at 12:26
  • I have gone and updated this info straight into the answer from Chip. It should now be complete. :-) – Pieter Goosen Dec 15 '14 at 12:36
0

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
    ) );
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.