• Resolved Suxa23

    (@suxa23)


    Hi, I’m trying to display in one section of my front page my latest posts from the last two days. The idea is to display a quick view of this week’s posts but just show the latest two past days and just the latest two posts of each day.

    So basically what I want to do is for example:

    Yesterday Date | -> last two posts with title, thumb, category.

    The day before Yesterday Date | -> last two posts with title, thumb, category.

    –> and link to see the whole week’s posts.

    So I tried doing a query, but after some hours of research and trial&Error. I was only able to display the day before yesterday’s post.

    This is the query I’m using:

    <?php
    			$arc_year = get_the_time('Y');
    			$arc_month = get_the_time('m');
    			$arc_day = get_the_time('d');
    			query_posts( 'year=' . $arc_year . '&month=' . $arc_month . '&day=' .  $arc_day . '&showposts=2');
    
    			if (have_posts()) : while (have_posts()) : the_post();?>

    I have no idea how to solve this, if someone can help me out I would really appreciate it, thanks!

    snapshoots (sorry dates are in spanish):

    what I want:
    http://swebdevelopment.com/clients/weekspost.png

    what I have:
    http://swebdevelopment.com/clients/sofar.png

Viewing 7 replies - 1 through 7 (of 7 total)
  • The Codex page on query_posts has a section on Time Parameters that shows an example of retrieving posts from 30 to 60 days old. You should be able to adapt that for 1 to 2 days old.

    Thread Starter Suxa23

    (@suxa23)

    I saw that code but at first glance I didn’t think I could make it work. I’ll take a closer look and see if I can get it to work and display the way I want, thanks for the tip!

    Thread Starter Suxa23

    (@suxa23)

    I’m really not getting anywhere, can someone please give me a bit more direction on how to use the Time Parameters 30 to 60 days, I can’t even get the example on the documentation to work. Do I need to change something once I paste it in my document? Do I need to add the “function filter_where( $where = ” )” on the functions.php how do I call it to my query_posts?

    I have spent so much time searching on the internet with no success, maybe I’m not searching correctly, is there any other ways or option to get to display the recent two posts of the past two days without hard-coding dates?

    Also, there’s suppose to be like up to 6 posts per day, that’s why I want just to show the latest two of each day.

    The code below should be easily adapted to your theme:

    // Create a new filtering function that will add our where clause to the query
       function filter_where( $where = '' ) {
       	// posts  1 to 2 days old
       	$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-2 days')) . "'" . " AND post_date < '" . date('Y-m-d') . "'";
       	return $where;
       }
       add_filter( 'posts_where', 'filter_where' );
    
       query_posts( 'post_type=post&posts_per_page=-1&caller_get_posts=1' );
       if ( have_posts() ) {
          while ( have_posts() ) {
             the_post();
             $this_date = get_the_time('Y-m-d');
             if (++$seen[$this_date] > 2) continue;
             // Put the code to display one post here
             echo "<h2>";the_title();echo '</h2>';
             echo the_time('F jS, y');
          }
       } else {
         // Code for no posts found
       }
    Thread Starter Suxa23

    (@suxa23)

    😀 wow awesome! Thank you so much, I would have never been able to figure it out by myself, my knowledge of coding is very basic.

    If you don’t mind may I pick your brain a bit more, if I want to display the date of the post like a “header type” just once before the two posts where would I insert the date code so it wouldn’t appear on both entries?

    This should be close to what you want:

    while ( have_posts() ) {
       the_post();
       $this_date = get_the_time('Y-m-d');
       if (++$seen[$this_date] > 2) continue;
       if ($seen[$this_date] == 1) {
          echo the_time('F jS, y');
          echo '<br />';
       }
       // Put the code to display one post here
       echo "<h2>";the_title();echo '</h2>';
    }
    Thread Starter Suxa23

    (@suxa23)

    Yes!! Thank you so much it works great!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to query the recent posts of the last two days of the week’ is closed to new replies.