Support » Themes and Templates » Showing an Excerpt of a particular day

  • Resolved weasello


    I want to be able to select a date, and have WordPress show only the first post from that day in excerpt form.

    I’m having trouble even starting, however – I can’t seem to figure out how wordpress selects what articles to post and whatnot.

    I could probably figure it out using direct SQL queries but I’m hoping it’s possible within WP’s environment.

    Can anyone show me a solution that would accomplish this?

Viewing 8 replies - 1 through 8 (of 8 total)
  • First, it never hurts to learn about The Loop.

    You can use query_posts() to initialize The Loop in such a manner:

    <?php query_posts('m=20060101&showposts=1'); ?>

    Or alternatively:

    <?php query_posts('year=2006&monthnum=01&day=01&showposts=1'); ?>

    Either will show the *last* post from January 1, 2006. To get at the first post of that day involves some WP/PHP trickery. First, we call up all posts from that day:


    Then we figure out where in the posts object the first post is hiding, and grab it’s post ID:

    $count = $wp_query->post_count - 1;
    $first_post_ID = $wp_query->posts[$count]->ID;

    The full bit of code together:

    $count = $wp_query->post_count - 1;
    $first_post_ID = $wp_query->posts[$count]->ID;

    Now for The Loop: this version checks to see if the current post ID is the *first” post ID:

    while(have_posts()) : the_post();
    if($post->ID == $first_post_ID) :

    From here we stick in all your Loop template tags and HTML and stuff. In this case make sure to use the_excerpt() in there somewhere:

    <?php the_excerpt(); ?>

    And wrapping up The Loop:

    <?php endif; endwhile; ?>

    Thanks for the tips! Very informative and I’ve learned a lot. Ran in to some trouble though.

    $querydate = date('Ymd',strtotime($provided_date));
    query_posts('m=' . $querydate . '&showposts=-1');
    $count = $wp_query->post_count - 1;
    $first_post_ID = $wp_query->posts[$count]->ID;

    That’s the code I’ve used, and with The Loop you mentioned above, it does indeed show an excerpt of EVERY post made that day. I inserted some echo commands, and it turns out that the failing line of code appears to be:

    $count = $wp_query->post_count - 1;

    This value, no matter what date is inserted, is always “-1”.

    Because of this, $first_post_ID always ends up being the same and the “if” statement is triggered for each post.

    I’m really scratching my head over this one!

    I just inserted into The Loop the command:


    And it turns out that both variables are listed as NULL, which is triggering the loop.

    Maybe I am referencing the variables incorrectly, or I’m not including a file properly?

    Uh oh, this is even more wacky now!!

    I havent’t figured out the above issues yet, but I’ve been working away at designing my site around it (hoping a solution will come along!)

    But this is just messed up. Right now my site is displaying ALL the posts from the selected day instead of just the first post, right? But the weird thing, is that it’s also displaying FUTURE, POST-DATED posts… But ONLY in internet explorer!!! FireFox isn’t showing them at all.

    I’m guessing this is a cookie-related issue as there’s no browser-specific code that can alter that type of thing. I’m hoping that shrinking it down to one post by fixing the above problems will make this go away, but just wanted to leave that interesting tidbit out there :/

    Can’t explain the IE/Firefox variance. May be a file cache issue, though.

    If echo($post->ID) is reporting null when in The Loop something really is wacky in your setup, as the $post object is available to all functions during a loop iteration. It should work…when one is in The Loop.

    Paste up the entire template you’re using here:

    and reply back with the url you receive. Hard to troubleshoot something like this blind.

    I’ll ignore those browser errors for now then 🙂

    My site layout is a little complicated which makes the code look a little more messy, so perhaps a visual would help as well. is a page that (should) show 7 individual posts from the previous 7 days. Using the code examples you gave, I constructed an external function (kinda like a plugin) that I could easily call for each of the days of the week.

    For example, for the MONDAY column, I simply put “sa_excerpt(‘Mon’);” into the index.php file and the code does the rest.

    A lot of the code I have in the pastebin right now is me figuring out what the date is for last monday and other custodial work. But like I said, I inserted an ECHO tag into Line 29.

    at Line 29, $count has a value of -1, and $first_post_ID has a value of *null*. As you can see by visiting my page, it is selecting the date and displaying the posts properly – it’s simply not selecting the first post properly, so the rest of my code *should* be good.

    Thanks a lot for the help!

    First, sorry for the delay. Real life and all.

    Second, there’s little you could stick in your template I can’t at least read through. Ok, that sounded a tad arrogant…

    Third, change your global to:

    global $post, $wp_query;

    That should fix things up.

    Not only did that work, but it fixed a few other bugs as well 🙂 thanks alot!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Showing an Excerpt of a particular day’ is closed to new replies.