• Resolved adkbasecamp

    (@adkbasecamp)


    I would like to display on Front Page a Daily Digest, limited to Last 3 Days. Ideally, I could also paginate with “previous entries”.

    Exmample:

    Friday 15 April 2011
    Post 2
    Post 1

    Friday 14 April 2011
    Post 2
    Post 1

    Here’s a screenshot of what I have using this code from @michaelh:

    [mod: please remember to use the pastebin for codes longer than 10 lines]

    <?php
    // list all posts, sectioned by post date
    $args=array(
      'orderby' => 'date',
      'order' => 'DESC',
      'posts_per_page'=>-1,
      'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'Date archive of all ' . count($my_query->posts) . ' posts';
      while ($my_query->have_posts()) : $my_query->the_post();
        $this_date =  get_the_time(__('l d F Y'));
        if ($this_date != $last_date) {
          $last_date = $this_date;
          echo '<h2>'.$last_date.'</h2>';
        } ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    } //if ($my_query)
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    This works beautifully, but displays All Dates. Can someone suggest how I could limit this to last X days? And perhaps also add pagination?

Viewing 6 replies - 1 through 6 (of 6 total)
  • kreativjustin

    (@kreativjustin)

    You could check against the age of the post, and if the age of the post is older, don’t display the post.

    I’ll write up something small for you.

    [mod: please remember to use the pastebin for codes longer than 10 lines]

    <?
    
    /* Credited to KreativJustin */
    
    	$post_date = the_date('Ymd','','',FALSE);
    	// We gathered original date from the post, let's strip it down so we can use it
    
    	$post_year = substr($post_date,0,4); // Years stripped. 0 from left, 4 chars max
    	$post_month = substr($post_date,4,2); // Months stripped. 5 from left, 2 chars max
    	$post_day = substr($post_date,6,2); // Months stripped. 7 from left, 2 chars max
    
    	// Now we need to gather info for the current date.
    	$current_date = date('Ymd');
    	// Great, let's strip down the current date. We can just copy, paste, modify the top one!
    
    	$current_year = substr($current_date,0,4); // Years stripped. 0 from left, 4 chars max
    	$current_month = substr($current_date,4,2); // Months stripped. 5 from left, 2 chars max
    	$current_day = substr($current_date,6,2); // Months stripped. 7 from left, 2 chars max
    
    	// Let's turn them into usable strings
    	$post_usage = $post_year . $post_month . $post_day;
    	$current_usage = $current_year. $current_month . $current_day;
    
    	$difference = $current_usage-$post_usage;
    
    	if($difference > "3") { // Days
    		// Too old, do nothing
    	} else {
    		// Perfect age, display post here.
    	}
    
    ?>

    This does work when inserted into a loop. I have used this code for other reasons within a post. Good luck!

    Thread Starter adkbasecamp

    (@adkbasecamp)

    Thanks man, I’ll give that at shot and see what kind of trouble I can get into.

    Where should I put that?

    kreativjustin

    (@kreativjustin)

    No problem, if you have any questions or comments, please reply.

    Thread Starter adkbasecamp

    (@adkbasecamp)

    @kreativejustin

    I have no clue how to use that, I’m barely literate when it comes to this stuff.

    Looking at that code you provided – it’s comparing the post date to current date, then if it is 3 days old or less, it will display the posts.

    How do I incorporate that into the code I have? Appreciate your help.

    Thread Starter adkbasecamp

    (@adkbasecamp)

    I believe this is resolved by adding a time parameter filter:

    function filter_where($where = '') {
        //posts in the last 3 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-3 days')) . "'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');

    (Hey Mod: Sorry about the code before)

    Daily Digest – Page Template – Last 3 Days

    My Page Template now displays the last 3 days of posts, listed under the date of posting. So cool, I love WP.

    To improve this, I would like to have it display the last 3 days with posts. Problem: if I skip 3 days of posting, nothing would display.

    If anyone has any comments about this implementation, improvements, etc. would welcome the input.

    (I would also like to add Page Navigation to previous or next “day” – limited to only 1 day’s digest per page. That’s likely going to be a support request for another day.)

    kreativjustin

    (@kreativjustin)

    Well heck, I didn’t even know you could do that with wordpress. I learned something new today, thanks adkbasecamp

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Limit Daily Digest to Last 3 Days’ is closed to new replies.