0

I'm a girl and excuse my English. Maybe its easy and I'm dumb to know it.

But here is the problem

I want to output on the homepage the posts from the lastest 3 days.

Like this:

30/03/12

post 1

post 2

post 3

29/03/12

post 1

post 2

post 3

28/03/12

post 1

post 2

post 3

I tried:

$loopday = '';
if ( have_posts() ) : while ( have_posts() ) : the_post();
  if ($loopday !== get_the_time('D M j')) {
    $loopday = get_the_time('D M j');
    echo $loopday;
  }
  the_title();
  the_content();
endwhile; endif;

But this code only orders the latest 10 posts. For example:

On 30/03/12 I have 5 posts

On 29/03/12 I have 4 posts

On 28/03/12 I have 3 posts

So the 3rd day is missing now 2 posts.

Also I want to use this on the archive page.

3 days a page.

On page 2 the next 3 days.

I'm really hopeless..

xx

1 Answer 1

1

Three loops

http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

$time = time();

foreach ( range( 0, 2 ) as $yesterday ):

    // get relative time offset by $yesterday days
    $rel_time = strtotime( "-$yesterday days", $time );

    // get the digits
    $year = date( 'Y', $rel_time );
    $day = date( 'd', $rel_time );
    $month = date( 'm', $rel_time );

    // get the posts
    foreach ( get_posts( 'year=' . $year . '&monthnum=' . $month . '&day=' . '$day&posts_per_page=3' ) as $post ):
        setup_postdata( $post );

        the_title();
        the_content();
    endforeach;

endforeach;

You can also pivot around the $time in other more efficient ways. Remember 3 loops = 3 database queries.

SQL

http://codex.wordpress.org/Class_Reference/wpdb

global $wpdb;
$sql = "SELECT * FROM `$wpdb->posts` WHERE 1=0";

$time = time();

for ( $i = 0; $i < 3; $i++ ) {
    $time = strtotime( "-1 day", $time ); // deduct one day every time

    $year = $wpdb->escape( date( 'Y', $time ) );
    $day = $wpdb->escape( date( 'd', $time ) );
    $month = $wpdb->escape( date( 'm', $time ) );

    $sql .= " OR `ID` IN ( SELECT `ID` FROM `$wpdb->posts` WHERE YEAR(`post_date`) = '$year' AND MONTH(`post_date`) = '$month' AND DAY(`post_date`) = '$day' LIMIT 3)";
}

foreach( $wpdb->get_results( $sql, OBJECT_K ) as $post ):
    setup_postdata( $post );
    // ...display stuff
endforeach;

This results in something like:

SELECT *
FROM `wp_posts`
WHERE 1=0
    OR `ID` IN (
        SELECT `ID`
        FROM `wp_posts`
        WHERE YEAR(`post_date`) = '2012'
            AND MONTH(`post_date`) = '03'
            AND DAY(`post_date`) = '30'
        LIMIT 3)
    OR `ID` IN (
        SELECT `ID`
        FROM `wp_posts`
        WHERE YEAR(`post_date`) = '2012'
            AND MONTH(`post_date`) = '03'
            AND DAY(`post_date`) = '29'
        LIMIT 3)
    OR `ID` IN (
        SELECT `ID`
        FROM `wp_posts`
        WHERE YEAR(`post_date`) = '2012'
            AND MONTH(`post_date`) = '03'
            AND DAY(`post_date`) = '28'
        LIMIT 3)

Big query, with 3 subqueries. Hairy.

Escape counters

$seendays = array(); // keep track of the day numbers

if ( have_posts() ) : while ( have_posts() ) : the_post();
    if ( isset( $seendays[get_the_date( 'Y-m-d' )] ) && $seendays[get_the_date( 'Y-m-d' )] > 3 ) continue;

    $seendays[get_the_date( 'Y-m-d' )] = isset( $seendays[get_the_date( 'Y-m-d' )] ) ? $seendays[get_the_date( 'Y-m-d' )] + 1 : 1;

    // ...display stuff
endif;

All three methods are quite raw, and will require some fine-tuning here and there. Hope this helps.

3
  • You'll have to add more filtering arguments, check out the documentation codex.wordpress.org/Template_Tags/get_posts
    – soulseekah
    Commented Mar 31, 2012 at 11:20
  • I used the first one. Works great, but how to use it on the archive page?.. Edit: Somedays I didn't post. How to skip this.. And still show 3 days.. For example.. If I didn't post on 28/03/12: 30/03/12 29/03/12 27/03/12
    – Christina
    Commented Mar 31, 2012 at 11:25
  • Use a while (true) loop instead of a foreach ( range( 0, 2 ) .. ) etc. and break when the total amount of posts gathered is 9. Try using the last one instead, it should work regardless of days, groups them by 3 or less.
    – soulseekah
    Commented Mar 31, 2012 at 11:28

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.