Writing my first theme from scratch here. I'm attempting to create a "read next" sidebar that will show the next 30 posts in a list on my sidebar in my single.php file. I know how to grab all of the recent posts like in the sample below that I used on my homepage, but I'm having trouble with this one.

I want the list to show the most recent 30 posts which were posted BEFORE the post the user is currently viewing. I'm assuming I need to use a different function or a different offset, but not sure how to go about getting "how far back" the current post is to use it as an offset. (If that's the right way to do it).

Take a look at this site for an example of what I'm trying to do like on her sidebar (underneath the ad on the right).

Here's the recent post arg array from my main page that I'm using as a template to create :

   $args = array(
        'numberposts' => 10,
        'offset' => 2,
        'post_type' => 'post',
        'post_status' => 'publish',
        'suppress_filters' => true );
        $recent_post_list = wp_get_recent_posts( $args, ARRAY_A );

And then in my output:

<?php foreach ($recent_post_list as $recent_post): ?>
    <div class="row">
        <div class="hidden-xs col-sm-1 col-md-1 col-lg-1"></div>
        <div class="col-xs-5 col-sm-3 col-md-5 col-lg-5">
            <?php if (has_post_thumbnail($recent_post->ID)): ?>
            <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $recent_post['ID'] ), 'single-post-thumbnail' ); ?>
            <a href="<?php echo get_permalink($recent_post['ID']);?>"><img class="img-responsive" src="<?php echo $image[0]; ?>" /></a>
            <?php endif; ?>

        <div class="col-xs-5 col-sm-7 col-md-5 col-lg-5">
                $category = get_the_category($recent_post['ID']);
            <span class="category-box hidden-xs"><?php echo $category[0]->cat_name; ?></span>
            <span class="category-box hidden-xs"><br /></span>
            <?php echo get_the_title( $recent_post['ID'] ); ?><br />
            <?php echo human_time_diff( get_the_time( 'U',$recent_post['ID']), current_time('timestamp') ) . ' ago'; ?>
        <div class="hidden-xs col-sm-1 col-md-1 col-lg-1"></div>    
    <div class="row">
        <div class="hidden-xs col-sm-1 col-md-1 col-lg-1"></div>
        <div class="col-xs-12 col-sm-10 col-md-10 col-lg-10"><hr /></div>
        <div class="hidden-xs col-sm-1 col-md-1 col-lg-1"></div>    

    <?php endforeach; ?>

Use a date_query to get the 30 posts older than the current one.


(NOTE: The following is untested and requires Wordpress 3.7+ and PHP 5.4+)

$current_post = get_queried_object();
$args = [
    // Your arguments to pass, add as needed
    'posts_per_page' => 30,
    'date_query' => [
            'before' => strtotime( $current_post->post_date ), // Add current post date to search posts againt before this one
            'inclusive' => false, // Exclude current post from the list
$q = get_posts( $args );
  • Not exactly sure why, but that didn't work. I did a little research on date_query and came up with this that worked: pastebin.com/T8P5fAYx Any ideas what I was doing wrong with yours? Jul 11 '15 at 22:43
  • Your php version is too old. My code will only work on PHP 5.4 due to the new short array syntax ([]). You should update your PHP version. All versions before 5.4 is EOL'ed which is a huge security risk if you are still using them Jul 12 '15 at 4:36
  • Any feedback on the issue? Have checked your code, and it almost matches mine with the exception of the array syntax Jul 12 '15 at 18:07

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.