What I have is a page which displayed a post from a category and at the bottom there's a previous and next post link. The problem I have is that when it gets to the last one, I want to loop back to the start. It works going to the end if you're on the first one, just not back to beginning if you're on the last, it's just displaying the last post if you're on the last post.
Here's my code so far:
<?php
$prev_post = get_adjacent_post( true, '', true );
$next_post = get_adjacent_post( true, '', false );
$prev_post_id = $prev_post->ID;
$next_post_id = $next_post->ID;
?>
<?php if ($prev_post_id == '') {
$query = new WP_Query( array ( 'orderby' => 'ASC', 'posts_per_page' => '1', 'cat=4' ));
while($query->have_posts()):
$query->the_post();
$prev_post_id = $query->post->ID;
endwhile;
} ?>
<?php if ($next_post_id == '') {
$query2 = new WP_Query( array ( 'orderby' => 'DESC', 'posts_per_page' => '1', 'cat=4' ));
while($query2->have_posts()):
$query2->the_post();
$next_post_id = $query2->post->ID;
endwhile;
} ?>
<a href="<?php echo get_permalink($prev_post_id); ?>" class="prev-footer-item">
<div class="prev-inner">
<h3 class="gamma"><?php echo get_the_title($prev_post_id ); ?></h3>
<h4 class="delta"><?php the_field('subtitle', $prev_post_id ); ?></h4>
<div class="footer-overlay"></div>
</div>
</a>
<a href="<?php echo get_permalink($next_post_id); ?>" class="next-footer-item">
<div class="next-inner">
<h3 class="gamma"><?php echo get_the_title($next_post_id); ?></h3>
<h4 class="delta"><?php the_field('subtitle', $next_post_id); ?></h4>
<div class="footer-overlay"></div>
</div>
</a>
I'm sure it's something quite obvious I'm missing, right?
Update: Working Prev Posts:
[request] => SELECT SQL_CALC_FOUND_ROWS as1_posts.ID FROM as1_posts WHERE 1=1 AND as1_posts.post_type = 'post' AND (as1_posts.post_status = 'publish' OR as1_posts.post_status = 'private') ORDER BY as1_posts.post_date DESC LIMIT 0, 1
Not Working Next Posts:
[request] => SELECT SQL_CALC_FOUND_ROWS as1_posts.ID FROM as1_posts WHERE 1=1 AND as1_posts.post_type = 'post' AND (as1_posts.post_status = 'publish' OR as1_posts.post_status = 'private') ORDER BY as1_posts.post_date DESC LIMIT 0, 1
'orderby' => 'ASC'seems to make no sense? E.g.,'orderby' => 'title', 'order' => 'ASC'? - user3445853