Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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">
                <?php
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $args= array(
                    'category_name' => 'offsite', 
                    'paged' => $paged,
                    'posts_per_page' => 1
                );
                query_posts($args);
                if( have_posts() ) :?>

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


                <article>
                    <?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>

                                    <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>
                                    </div>
                                </div>

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

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

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

                  </article>


                <?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(); ?>
                </div>
share|improve this question
    
The solution to this is so simple and can be found here: stackoverflow.com/questions/9741376/… –  egr103 Mar 17 '12 at 12:01
add comment

1 Answer

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' );
share|improve this answer
    
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 '12 at 10:12
    
Did you just copy paste it, or did you actually put in your own variables and such for it? –  m0r7if3r Mar 17 '12 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 '12 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. –  m0r7if3r Mar 17 '12 at 16:19
add comment

Your Answer

 
discard

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.