Support » Plugins and Hacks » Posts in the last X minutes/hours

  • bjc999


    Hi there, noob here… would be very grateful for any help!

    I’m trying to achieve the following…

    On my home page I wish to display the recent posts in time segments

    i.e. posts in the last 5 minutes, posts in the last 15 minutes, posts in the last 30 minutes, posts in the last 1 hour, posts in the last 2 hours and so on and so forth…

    Very much like NewsNow:

    Many thanks in advance!

Viewing 1 replies (of 1 total)
  • MichaelH


    Didn’t test this too much:

    // get all posts, if posts less than two hours old then display a section for 5 min, 10 min, 15 min etc and if post qualifies display it.
    // assumes posts are returned newest to oldest
    $minutes = array (0,5,10,15,30,60,120); //minutes for each 'section'
    $count_of_minutes = count($minutes);
    //derive actual date time for each section
    $dates = array(); //
    foreach ($minutes as $minute) {
    $dates[] = date('Y-m-d G:i:s', strtotime('-'.$minute.' minutes'));
    $oldest_date = date('Y-m-d G:i:s', strtotime('-'.$minutes[$count_of_minutes-1].' minutes'));
    $titles = array ('','5 minutes','10 minutes', '15 minutes','half-hour', 'hour', '2 hours');
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'Listing of posts ';
      while ($my_query->have_posts()) : $my_query->the_post();
        $post_date = $my_query->post->post_date_gmt;
        // only consider posts that are no older than 2 hours
        if ($post_date >= $oldest_date) {
          // iterate through each 'date' section and if post qualifies display the 'section/date' heading first, then the post title
          for ( $counter = 1; $counter < $count_of_minutes; $counter += 1) {
            if ($post_date >= $dates[$counter] && $post_date < $dates[$counter-1] ) {
              // test if title has not been displayed--if so do it and clear it so it won't print again
              if ( $titles[$counter] ) {
                echo '<H2> Posts in the last ' .$titles[$counter] . '</H2>';
                $titles[$counter] = '';
              <p><small><?php the_time('m.d.y G:i:s') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    wp_reset_query();  // Restore global post data stomped by the_post().
Viewing 1 replies (of 1 total)
  • The topic ‘Posts in the last X minutes/hours’ is closed to new replies.