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(); ?>
$queryObject
use$wp_query
. Look into post for reason wordpress.stackexchange.com/questions/120407/…$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()