WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I really need your help.

in my single.php I had to get the next post in the same category

(which i already have by:

$in_same_cat = true;

$excluded_categories = '';

$previous = false;

$next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);)

now I need the next next post and also in the opposite direction previous previous post


share|improve this question

See answer here for some code that fetches several adjacent posts:

Getting the Next and Previous Posts Titles in the Sidebar?

share|improve this answer

Try to use the get_adjacent_post function according to this link
you can create a small loop that will get the ID of the next post and then use the function again on the ID that you've received to get the next next ID.

$prevPost = get_previous_post();
$i = 0;  
$num_prev_posts = 4;  
while ($i < $num_prev_posts) //needs to check if $prevPost exists. while ($i < $num_prev_posts && !empty($prevPost)) doesn't work. 
    get_permalink($post->ID); //and other such functions that work on the global $post should now work.  
    $prevPost = get_previous_post(); // and then there should be a check if $prevPost is empty or not  

this will bring the previous 4 posts. http://wordpress.org/support/topic/get-next-5-posts-from-the-current-post

share|improve this answer
up vote 2 down vote accepted

surprisingly,I found the answer myself...

I am using the same function as I used for next/previous post

(get_adjacent_post() ) but sending the the next/previous post which I already found as a parameter

$in_same_cat = true;

$excluded_categories = '';

$previous = true;

$previous_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);

$previous_previous_post = 

$previous = false;

$next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);

$next_next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous,$next_post); 

but...we haven't finished yet.. we need to add this code to the function declaration in wp-includes/link-template.php

function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true,$mypost = null) {
    global  $wpdb;

//if specific post wasnt sent to function it takes the global one and checks if its empty before using it.

    if ( empty( $mypost ) )
        global $post;
        if(empty( $post ))
            return null;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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