The way I would like my blog to work is a user clicks on a post that was published on 12th March 2012 (for example) they get taken to single.php where the content of that post is displayed as normal.

Beneath this content I would like to display all posts published before it, not after. How can I edit my existing loop to do this? Here's my working loop that currently displays all posts, no matter what the date:

<!-- Show all posts within this category -->
                <div id="post-wrap">
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $args= array(
                    'category_name' => 'offsite', 
                    'paged' => $paged,
                    'posts_per_page' => 1
                if( have_posts() ) :?>

                    <?php while ( have_posts() ) : the_post(); ?>

                    <?php the_post_thumbnail(); ?>
                    <h1><a href="<?php the_permalink()?>"><?php the_title(); ?> <span>/ <?php the_time('l jS F') ?></span></a></h1>

                    <?php the_content(''); ?>

                    <div class="a-options">
                        <a href="#new" class="comments scrollto">Comment</a>

                        <!-- share -->
                            <div class="share-button">
                                <div class="buttons">
                                    <div class="facebook">                          

                                        <div class="fb-like" data-href="www.MyKettlesYard.co.uk" data-send="false" data-layout="button_count" data-width="95" data-show-faces="false" data-font="Arial"></div>

                                        <span class="child-pseudo">Facebook</span>

                                    <div class="twitter">

                                        <a href="https://twitter.com/share" class="twitter-share-button" data-url="www.MyKettlesYard.co.uk" data-text="Kettles Yard, Cambridge - www.MyKettlesYard.co.uk" data-count="horizontal">Tweet</a><script src="//platform.twitter.com/widgets.js"></script>

                                        <span class="child-pseudo">Twitter</span>

                                <span class="pseudo-button">&#043; Share</span>
                             </div> <!--! /share -->

                    </div><!--! /a-options -->

                    <?php global $withcomments;
                    $withcomments = 1;
                    comments_template(); ?>


                <?php endwhile; ?>

                    <div id="next-prev-wrap" class="nextprev"><?php next_posts_link( 'Older posts /', $post->max_num_pages ); ?> <?php previous_posts_link( 'Newer posts', $post->max_num_pages ); ?></div>

                <?php endif; ?>
                <?php wp_reset_query(); ?>

1 Answer 1


How about something along these lines?

function my_filter_where( $where = '' ) {
    global $post;

    $where .= ' AND post_date <= "'.$post->post_date.'"';
    return $where;

add_filter( 'posts_where', 'my_filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'my_filter_where' );
  • I have put this both within my functions.php file and also tried it at the top of my single.php file (the file i'd like it applied to) but it doesn't sem to work. Any ideas?
    – egr103
    Mar 16, 2012 at 10:12
  • Did you just copy paste it, or did you actually put in your own variables and such for it?
    – mor7ifer
    Mar 17, 2012 at 2:43
  • I have literally just found a simple answer: the solution to my above code can be found here: stackoverflow.com/questions/9741376/…
    – egr103
    Mar 17, 2012 at 12:00
  • The downside to that is going to be that you're getting a lot more data than you're using, especially in the earlier posts. If you're working with a site that you want to have good scalability, it would be in your best interests to do it in a way that doesn't filter the data once it's been queried, but rather filters before the query.
    – mor7ifer
    Mar 17, 2012 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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