2

I am making a pagination for this website by using a custom query and get_next_posts_link, get_previous_posts_link. The problem is that the link to older entries (get_next_posts_link) only works once, meaning that if I click on it the second time, it will always lead to the home page, this is weird because when I inspect the link, the href attribute is: http://localhost:8888/athena/event/page/3.

There are 7 pages according to the variable $queryObject->max_num_pages

A small screen capture video to show what I mean (27 seconds long): https://www.useloom.com/share/f8f9ecac9dd54a49aa3613f9c0f5c9f9

Here's my code:

    <!-- section list events-->
    <?php
      if(get_query_var('paged')){
        $paged = get_query_var('paged');
      } elseif (get_query_var('page')) {
        $paged = get_query_var('page');
      } else {
        $paged = 1;
      }
      $query_args = array(
        'post_type' => 'event',
        'posts_per_page' => 3,
        'paged' => $paged
      );
      $queryObject = new WP_Query($query_args);
     ?>

    <section class="block-list-events">
      <div class="container">
        <div style="color: #000;">
          <?php var_dump($queryObject->found_posts); ?>
        </div>

        <div class="list-events">
        <?php if ($queryObject->have_posts()): while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
          <div class="item clearfix">
            <div class="img tbl pull-left">
              <div class="tbl-cell date">
                <p><?php the_time('Y M') ?></p>
                <p><span><?php the_time('j') ?></span></p>
              </div>
              <div class="tbl-cell img-a">
                <a href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url('event-single'); ?>" width="530" height="300" alt="<?php the_title(); ?>"/></a>
              </div>
            </div>
            <div class="info pull-left">
              <p class="tag"><?php the_field('label'); ?></p>
              <h4><a href="<?php echo get_the_permalink() ?>"><?php the_title(); ?></a></h4>
              <p class="desc"><?php echo excerpt(25); ?></p>
              <div class="button-view-detail">
                <a class="btn btn-3" href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>">View Details</a>
              </div>
            </div>
          </div>
         <?php endwhile; ?>
        </div>
        <?php endif; ?>

        <div class="clearfix">
      <!-- Pagination -->
      <?php if ($queryObject->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
        <nav class="prev-next-posts">
          <div class="prev-posts-link">
            <?php echo get_next_posts_link( 'Older Entries', $queryObject->max_num_pages ); // display older posts link ?>
          </div>
          <div class="next-posts-link">
            <?php echo get_previous_posts_link( 'Newer Entries', $queryObject->max_num_pages ); // display newer posts link ?>
          </div>
        </nav>
      <?php } ?>
    </div>

    </section>
    <!-- /end of section list events -->
  </main>

<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>

As suggested by @amit, I've updated my code but still the result is the same as before:

    <!-- section list events-->
    <?php

      if(get_query_var('paged')){
        $paged = get_query_var('paged');
      } elseif (get_query_var('page')) {
        $paged = get_query_var('page');
      } else {
        $paged = 1;
      }
      $query_args = array(
        'post_type' => 'event',
        'numberposts' => -1,
        'posts_per_page' => 3,
        'paged' => $paged
      );
      $queryObject = new WP_Query($query_args);

      // Pagination fix
      $temp_query = $wp_query;
      $wp_query   = NULL;
      $wp_query   = $queryObject;
     ?>

    <section class="block-list-events">
      <div class="container">
        <div style="color: #000;">
          <?php var_dump($queryObject->found_posts); ?>
        </div>

        <div class="list-events">
        <?php if ($queryObject->have_posts()): while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
          <div class="item clearfix">
            <div class="img tbl pull-left">
              <div class="tbl-cell date">
                <p><?php the_time('Y M') ?></p>
                <p><span><?php the_time('j') ?></span></p>
              </div>
              <div class="tbl-cell img-a">
                <a href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url('event-single'); ?>" width="530" height="300" alt="<?php the_title(); ?>"/></a>
              </div>
            </div>
            <div class="info pull-left">
              <p class="tag"><?php the_field('label'); ?></p>
              <h4><a href="<?php echo get_the_permalink() ?>"><?php the_title(); ?></a></h4>
              <p class="desc"><?php echo excerpt(25); ?></p>
              <div class="button-view-detail">
                <a class="btn btn-3" href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>">View Details</a>
              </div>
            </div>
          </div>
         <?php endwhile; ?>
        </div>
        <?php endif; ?>

    <?php
      // Reset postdata
      wp_reset_postdata();
    ?>

        <div class="clearfix">
      <!-- Pagination -->
      <?php if ($queryObject->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
        <nav class="prev-next-posts">
          <div class="prev-posts-link">
            <?php echo get_next_posts_link( 'Older Entries', $queryObject->max_num_pages ); // display older posts link ?>
          </div>
          <div class="next-posts-link">
            <?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
          </div>
        </nav>
      <?php } ?>
    </div>

    </section>
    <!-- /end of section list events -->
  </main>
<?php
  // Reset main query object
  $wp_query = NULL;
  $wp_query = $temp_query;
?>
<?php get_footer(); ?>
12
  • 1
    instead of $queryObject use $wp_query. Look into post for reason wordpress.stackexchange.com/questions/120407/…
    – BlueSuiter
    Commented May 4, 2017 at 6:59
  • @AmitChauhan I have changed the name of the query variable, however that doesn't solve my problem
    – fatg
    Commented May 4, 2017 at 7:01
  • @AmitChauhan I have updated the code
    – fatg
    Commented May 4, 2017 at 7:22
  • You did it wrong $queryObject = new WP_Query($query_args); supposed to be $wp_query = new WP_Query($query_args); and $queryObject->have_posts() to be $wp_query->have_posts()
    – BlueSuiter
    Commented May 4, 2017 at 7:23
  • 1
    @JackJohansson Yes I tried that plugin and it worked perfectly but it doesn't allow modifying the structure so I have to make a custom one (YaphetS fanboy here :D my real name is actually Gia Phat => yaphats)
    – fatg
    Commented May 4, 2017 at 10:58

2 Answers 2

1

Pagination is unlikely to work on anything which is not the main query and there is not much point to try to force it to work in other places. Use pre_get_posts filter if modifying the main query will give you good results, otherwise just invent your own pagination scheme that do not use the same parameters as wordpress.

2
  • 1
    could you kindly provide a small example on using pre_get_posts in this situation? I'm sorry but I'm fairly new to wordpress.
    – fatg
    Commented May 4, 2017 at 7:57
  • this wordpress.stackexchange.com/questions/44672/… has a very easy to understand example of the usage of the filter. Doesn't seem like there is a very good explanation of its general use but there are many usage examples on this site. For your specific issue there is not enough context and therefor hard to give a more useful example, but lets say you want to control the number of events on the page you use the filter to set the `'posts_per_page' parameter of the query (btw the number of posts and post pet page might conflict) Commented May 4, 2017 at 8:16
1
<!-- section list events-->
<?php

  if(get_query_var('paged')){
    $paged = get_query_var('paged');
  } elseif (get_query_var('page')) {
    $paged = get_query_var('page');
  } else {
    $paged = 1;
  }
  $query_args = array(
    'post_type' => 'event',
    'numberposts' => -1,
    'posts_per_page' => 3,
    'paged' => $paged
  );
  $queryObject = $wp_query;
  $queryObject = new WP_Query($query_args);

 ?>

<section class="block-list-events">
  <div class="container">
    <div style="color: #000;">
      <?php var_dump($queryObject->found_posts); ?>
    </div>

    <div class="list-events">
    <?php if ($queryObject->have_posts()): while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
      <div class="item clearfix">
        <div class="img tbl pull-left">
          <div class="tbl-cell date">
            <p><?php the_time('Y M') ?></p>
            <p><span><?php the_time('j') ?></span></p>
          </div>
          <div class="tbl-cell img-a">
            <a href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url('event-single'); ?>" width="530" height="300" alt="<?php the_title(); ?>"/></a>
          </div>
        </div>
        <div class="info pull-left">
          <p class="tag"><?php the_field('label'); ?></p>
          <h4><a href="<?php echo get_the_permalink() ?>"><?php the_title(); ?></a></h4>
          <p class="desc"><?php echo excerpt(25); ?></p>
          <div class="button-view-detail">
            <a class="btn btn-3" href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>">View Details</a>
          </div>
        </div>
      </div>
     <?php endwhile; ?>
    </div>
    <?php endif; ?>

    <div class="clearfix">
  <!-- Pagination -->
  <?php if ($queryObject->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
    <nav class="prev-next-posts">
      <div class="prev-posts-link">
        <?php echo get_next_posts_link( 'Older Entries', $queryObject->max_num_pages ); // display older posts link ?>
      </div>
      <div class="next-posts-link">
        <?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
      </div>
    </nav>
  <?php } ?>
</div>

<?php
  // Reset postdata
  wp_reset_postdata();
?>

</section>
<!-- /end of section list events -->

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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