• It’s a bit unusual, I know, but I’m designing a site in which I want posts, when displayed as single posts, to always display below them whatever post immediately preceded them chronologically in the same category, in order to always keep a sort of flowing, ongoing feel to posts.

    But it’s proven trickier than I’d imagined.

    Right now, I’ve basically got this:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php ($current = get_the_id()) ?>

    [display post, end loop, etc…]

    and then:

    <?php $blog_query = new WP_Query('p=' . $current . '&offset=1');
      while ($blog_query->have_posts()) : $blog_query->the_post(); ?>

    But the offset command doesn’t work that way. Is there anything that can manipulate the Post_ID I’m summoning to pull the preceding Post_ID (in the same category)? Any other ideas of how to go about this?

Viewing 1 replies (of 1 total)
  • Thread Starter pgmccullough

    (@pgmccullough)

    Okay–well, I got this working how I wanted, but probably in the most unnecessarily convoluted way possible. Surely there’s got to be a more efficient way! At any rate, if anyone else finds themselves at a loss as to how to do this, here’s what I managed to come up with.

    First, we summon the main post with a basic loop. While in the loop, we find out what the post id of the main post is:

    <?php ($current = get_the_id()) ?>

    Now comes the funny stuff. I get a full list of all the posts I have in the given category (I wanted the previous post in the same category). This gives me an array of post ids. I then run a check that basically looks through each post id and checks to see if it matches the post id we pulled from the main post. If it is, we tell it to grab the following post id in the array.

    <?php $counter = 1; ?>
    <?php
    global $post;
    $args = array( 'numberposts' => 100, 'offset'=> 0, 'category' => 9 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post); ?>
    <?php ($nextpost[$counter] = get_the_id()); ?>
    <?php $counter++; ?>
    <?php endforeach; ?>
    <?php ($hwmnypsts = count($nextpost)); ?>
    <?php $countsy = 1; ?>
    <?php while($countsy <= $hwmnypsts) : ?>
    <?php if ($nextpost[$countsy] == $current) {
    $countsy++;
    $next = $nextpost[$countsy];
    } else {
    $countsy++;
    } ?>
    <?php endwhile; ?>

    now that we’ve got the next post id, we just have to call the post up:

    <?php $last_query = new WP_Query('p=' . $next);
      while ($last_query->have_posts()) : $last_query->the_post(); ?>

    And finally, I through in a little extra to make sure that if there is no next post id (as in, we’re looking at the earliest post), it doesn’t load a next post.

    I did that by simply surrounding the content of the second post with:

    <?php if (isset($next)) : ?>

    Hope someone else is able to use this!

Viewing 1 replies (of 1 total)
  • The topic ‘Pulling single post AND post prior to that post’ is closed to new replies.