• pk-71

    (@pauljohnknight)


    Hi,

    Is it possible when using the previous_post_link() function to make it get the previous post after next i.e. make it skip a post. So if you’re on post 5 in numerical order it skips to post 3?

    The reason being I have custom post type single-cpt.php file that pulls in two posts, so when using the previous post link out of the box it means when you go to the next post, one of the ones from the previous post is there again.

    Thanks in advance for any help / ideas.

Viewing 4 replies - 1 through 4 (of 4 total)
  • piggy backing on the code here: https://www.ads-software.com/support/topic/pull-in-two-posts-to-custom-post-type-cpt-single-php-file-with-wp_query/ as it uses some of the same variables…

    	$all_ids = get_posts(array(
    		'fields'          => 'ids',
    		'posts_per_page'  => -1,
    		'post_type' => $cpt,
    		'order_by' => 'post_date',
    		'order' => 'ASC',
    	));
    	$prev_key = array_search($current_id, $all_ids) - 2;
    	$next_key = array_search($current_id, $all_ids) + 2;
    	if(array_key_exists($prev_key, $all_ids)){
    		$prev_link = get_the_permalink($all_ids[$prev_key]);
    		echo $prev_link;
    	}
    	if(array_key_exists($next_key, $all_ids)){
    		$next_link = get_the_permalink($all_ids[$next_key]);
    		echo $next_link;
    	}

    So I queried all the posts IDs from the current post type. Then since it’s a simple array key=>value, just found the current post key in the array and just added or subtracted so if your current post is the 8th one, your next is 10 and you previous is 6. Then if your array doesn’t have enough keys, say your current was the 8th but your array only had 9 that will mess it up, I check to see if the key exists. If it does, use get_the_permalink() with the value of the desired key.

    Probably not the most graceful way to do it but…

    • This reply was modified 4 years, 6 months ago by tugbucket.
    Thread Starter pk-71

    (@pauljohnknight)

    Thanks that worked. I only need the previous posts and tweaked it so I could incorporate it into an anchor link as per below. I can’t thank you enough, coming from someone who still has plenty to learn it’s been a godsend.

    <?php
    
        $all_ids = get_posts(array(
            'fields'          => 'ids',
            'posts_per_page'  => -1,
            'post_type' => $cpt,
            'order_by' => 'post_date',
            'order' => 'ASC',
        ));
        $prev_key = array_search($current_id, $all_ids) - 2;
        $next_key = array_search($current_id, $all_ids) + 2;
        if(array_key_exists($prev_key, $all_ids)){
            $prev_link = get_the_permalink($all_ids[$prev_key]); ?>
    
            <a href="<?php echo $prev_link; ?>">Next Post</a>
        
    <?php } ?>

    Sweet glad it worked for you.

    Thread Starter pk-71

    (@pauljohnknight)

    Hi @tugbucket I meant to say as well if you cut and paste this answer in the stackoverflow question (link below) we’ll happily assign you as the correct answer again. I forgot to mention that. Thanks again

    https://wordpress.stackexchange.com/questions/372764/make-previous-post-link-function-show-the-post-after-next-i-e-jump-a-post

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Make `previous_post_link()` Function Show The Post After Next i.e. Jump A Post’ is closed to new replies.