0

I want to get the IDs of the 2 previous and 2 next articles in a certain category to show them as a "related videos" playlist. I know that there are the functions previous_post_link and next_post_link, which would do the job for me, but I want more than just 1 previous and 1 next link.

Imagine the following scenario:

  • Post #001
  • Post #002
  • Post #003
  • Post #004
  • Post #005
  • Post #006

On Post #001 I want to display #002 and #003. On Post #003 I want to display #001, #002, #004 and #005.

$post_id = $post->ID; // current post id

$args = array('category' => $mycat, 'numberposts' => 200);
$posts = get_posts($args);
// get ids of posts retrieved from get_posts
$ids = array();
foreach ($posts as $thepost) {
    $ids[] += $thepost->ID;
}
// get and echo previous and next post in the same category
$thisindex = array_search($post->ID, $ids);
$prevvid = $ids[$thisindex-2];
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];
$nexttid = $ids[$thisindex+2];

I adapted and changed this code from the Wordpress Codex.

And now the part that worries me: If I don't add a large number to numberposts, it will of course only fetch the standard amount of posts and therefore my code won't work the way I want it. I probably will never ever have more than 200 videos in one category. Do you think my solution is a bad idea regarding performance? I didn't find a trick elsewhere to show the adjacent posts.

Thanks in advance for any advice!

  • 1
    Why you should want to fetch 200 posts if you are going to show 4 related posts maximum? Of course it is a bad idea: always fetch only the data you need. Instead of query for all posts and search in the results, make the correct query to get only the results you want. Also, you could run get_adjacent_post four times or get_next_post, which I think would be faster that query for 200 posts. – cybmeta Dec 18 '14 at 12:32
  • How can I modify the functions get_adjacent_post and get_next_post to give me two adjacent and two next posts? Or do you mean I have to apply a filter or a custom database query, because if so, I have no clue no how to get exactly those 4 posts I need. Thanks for your answer! Edit: Oh wait, so you mean I use get_next_post to get the ID of the next post, and then use that function again? I think that would work. Thanks for your input, that really makes sense now! I will try that and post my results here. – Schilling Dec 18 '14 at 15:32
0

If you want to query for a large set of posts, but don't need full information for each you can use 'fields' => 'ids'm see Return Fields Parameter.

In this case, I concur with comment, it might make more sense to simply retrieve adjacent post several times. You'll have to use setup_postdata() and wp_reset_postdata() since get_adjacent_post() doesn't allow you to set post explicitly. Unfortunately that function isn't too friendly to reuse or make use of SQL it generates.

  • Thanks alot, Rarst! This is how I got it to work and now there shouldn't be problems compared to my first codesnippet. I didn't think of using get_adjacent_post() in a post which I already fetched through get_adjacent_post(), but it worked. – Schilling Dec 18 '14 at 18:46
1

Use 'offset' in your query to start two posts before the current one. So if you're iterating through posts and the current post is number 14, then offset by 12 and ask for 5 posts. This will give you numbers 12-16 which is what you need and nothing more.

$args = array(
  'category' => $mycat, 
  'numberposts' => 5,
  'offset' => 12,
);
$posts = get_posts( $args );
  • I though also about offset in combination with post__not_in to exclude current post, but I think in the context of the question the position of current post is unknown. – cybmeta Dec 18 '14 at 16:07
  • Thank you Charles for your answer. I think this would be the perfect solution, but I got it to work with using get_adjacent_post() two times for my next and two times for my previous posts, which is still not as simple as your code, but my problem was finding the right offset. As @cybmeta said, I don't know at which place in the category my current post is, so I can't think of a way to get the right offset value. But then again I'm not a pro at PHP so someone might get this to work. – Schilling Dec 18 '14 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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