0

I am building a site client who makes anywhere from 20-50 news posts per day. I'd like to have a news feed where it shows the date / post count as an H3 and then list all the posts for that day underneath.

Example...

TODAY (4 posts) - News Article Link - News Article Link - News Article Link - News Article Link

YESTERDAY (6 posts) - News Article Link - News Article Link - News Article Link - News Article Link - News Article Link - News Article Link

MONDAY, JUNE 22, 2015 (3 posts) - News Article Link - News Article Link - News Article Link

...I can't find info anywhere on Google. Hoping this is possible. Thanks to anyone who can help.

Justin.

0

Clever manipulation of your Loop code should prevent having to make numerous additional requests to the database. Proof of concept:

$args = array(
  'post_type' => 'post'
);
$qry = new WP_Query($args);

if ($qry->have_posts()) {
  $date = $count = 0;
  $content = '';
  while ($qry->have_posts()) {
    $qry->the_post();
    $tdate = mysql2date('Y-m-d',$post->post_date);
    if ($date != $tdate) {
      if ($date != 0) {
        echo $date.' ('.$count.' Posts)';
        echo '<br>';
        echo $content;
        echo '<br>'.str_repeat('-',50).'<br>';
      }
      $content = '';
      $count = 0;
      $date = $tdate;
    }
    $content .= '<br>'.get_the_title();
    $count++;
  }
}
0

Try this function, will get all posts from '$day' days before today

/*
 * Get all posts from a specific day
 *
 * $day = how many days before today
 *
 * usage:   posts_from_the_day() will show posts from today
 *          posts_from_the_day(1) will show posts from yesterday
 *          posts_from_the_day(30) will show posts from 30 days ago
 *
 */
function posts_from_the_day( $day = 0 ) {

    $now = date('U'); //Get current second time
    $desired = $now - ( $day * 86400 ); //86400 = seconds in a day

    $year = date('Y', $desired);
    $month = date('m', $desired);
    $day = date('n', $desired);

    $query = new WP_Query( array(
        'year' => $year,
        'monthnum' => $month,
        'day' => $day,
        'posts_per_page' => -1,
    );
    if ($query->have_posts() ) {

        echo '<h3>'. date('L, F d, Y', $desired) . ' ('. $query->found_posts .') </h3>';

        while ( $query->have_posts() ) {
            $query->the_post();

            //do something

        }

    }

}
  • This is going to be very expensive to run, and you are going to repeat yourself a lot. Please see the DRY principle – Pieter Goosen Jun 29 '15 at 7:39
  • I agree! But I've made this code more in a easy-to-read and multi-purpouse way than top-notch performance and specific-focused way. But even with this function he can get high performance with cache and transient ;) – Bruno Cantuaria Jun 29 '15 at 11:54

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.