0

I want to add pagination to my custom page in a popular post, this is my coding.

   <div class="contentpost">
  <div id="rls" class="lr">
    <?php
    $popularpost = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 10, 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num'));
    while ($popularpost->have_posts()) : $popularpost->the_post();
       ?>
     <div class="epshen">
        <div class="bx">
           <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
              <div class="thumb">
                 <div class="img">
                   <?php if (has_post_thumbnail()) {
                         the_post_thumbnail();
                      } ?>
                 </div>
                 <div class="hover"><span class="dashicons dashicons-controls-play"></span></div>
              </div>
              <div class="ix">
                 <div class="title"><?php the_title(); ?></div>
                 <span><?php echo getPostViews(get_the_ID()); ?> - <?php the_time('l, F jS, Y'); ?></span>
              </div>
           </a>
           <h2><?php the_title(); ?></h2>
        </div>
     </div>
   <?php endwhile; ?>
   <!-- End Popular Series -->
  </div>

  • 1
    I see you used WP_Query, is there any reason you didn't use the pre_get_posts hook to modify the main query? If you'd modified the main query pagination would just work as is without any effort, and you'd be making half the queries so it'd be much faster – Tom J Nowell Nov 4 '19 at 1:43
  • Also keep in mind that storing page view counts in post meta will be both slow and unreliable. It'll mean database writes on every page load limiting the number of concurrent visitors, and, it'll mean you're massively undercounting page views due to race conditions. Ideally you would pull the data in from a proper analytics service such as Google Analytics on a cron job, and store the result in post meta, with it updating every 5-10 minutes – Tom J Nowell Nov 4 '19 at 2:11
  • Hi Please refer to this one, this solution will work for you. – divyaT Nov 4 '19 at 8:26
  • thanks for your input i will try it. – No Name Nov 4 '19 at 10:11

Your Answer

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

Browse other questions tagged or ask your own question.