0

I'm trying to get the posts that was updated from today, yesterday and 2 days ago but 2 days ago is showing the result of yesterday. What seems wrong with my code? Today and Yesterday is working fine. And also the post that query showing from yesterday and 2 days ago are only updated once and it was yesterday. I need help I'm a little bit confuse.

<span>Today's Update (<?php echo date('F j, Y'); ?>)</span>
<?php

$args = array (
    'post_type'          => array( 'post' ),
    'post_status'        => array( 'publish' ),
    'category__in'       => array( 3,7  ),
    'date_query'         => array(
        array(
            'column'     => 'post_modified_gmt',
            'after'      => 'today',
            'inclusive'  => true        ),
    )
);
$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li><a href="'.get_permalink().'">' . get_the_title() . '</a></li>';
    }
    echo '</ul>';
} else {
    echo "<ul><li>No Update</li></ul>";
}
/* Restore original Post Data */
wp_reset_postdata();
?>

<span>Yesterday Update (<?php echo date('F j, Y',strtotime("-1 days")); ?>)</span>
<?php

$args2 = array (
    'post_type'          => array( 'post' ),
    'post_status'        => array( 'publish' ),
    'category__in'       => array( 3,7  ),
    'date_query'         => array(
        array(
            'column'     => 'post_modified_gmt',
            'after'      => 'yesterday',
            'inclusive'  => true     ),
    )
);
$the_query2 = new WP_Query( $args2 );

if ( $the_query2->have_posts() ) {
    echo '<ul>';
    while ( $the_query2->have_posts() ) {
        $the_query2->the_post();
        echo '<li><a href="'.get_permalink().'">' . get_the_title() . '</a></li>';
    }
    echo '</ul>';
} else {
    echo "<ul><li>No Update</li></ul>";
}
/* Restore original Post Data */
wp_reset_postdata();
?>

<span>2 Days ago Update (<?php echo date('F j, Y',strtotime("-2 days")); ?>)</span>
<?php

// 2 Days ago post modified.
$args3 = array (
    'post_type'          => array( 'post' ),
    'post_status'        => array( 'publish' ),
    'category__in'       => array( 3,7  ),
    'date_query'         => array(
        array(
            'column'     => 'post_modified_gmt',
            'after'      => '-2days',
            'inclusive'  => true        ),

    )
);
$the_query3 = new WP_Query( $args3 );

if ( $the_query3->have_posts() ) {
    echo '<ul>';
    while ( $the_query3->have_posts() ) {
        $the_query3->the_post();
        echo '<li><a href="'.get_permalink().'">' . get_the_title() . '</a></li>';
    }
    echo '</ul>';
} else {
    echo "<ul><li>No Update</li></ul>";
}
/* Restore original Post Data */
wp_reset_postdata();
?>
6
  • Why are you running 3 separate loops? Commented Sep 26, 2015 at 7:03
  • @PieterGoosen I'm still not used to for loop, and I saw from codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops that its possible and I just did. Commented Sep 26, 2015 at 7:21
  • Don't always trust what the codex says, ;-). That specific page contains a lot of crap. I would do one loop and get all posts from the required couple of days Commented Sep 26, 2015 at 7:22
  • since you asked about 3 loops I did 2 loops and thought it was because of that. I did 2 loops and the problems is still there, from yesterday to 2days ago only. Commented Sep 26, 2015 at 7:24
  • Unfortunately I cannot post a solution now, but do one query, look at relative time/date frames valid in a date_query (PHP) Commented Sep 26, 2015 at 7:28

1 Answer 1

1

As I said in comments, you can do this in one query by making use of the relative time and date functionality in PHP which is also vailable in a date_query. You simply need to pass something like 2 days ago to the after parameter in the date_query to get posts from today, yesterday and two days ago. Just remember to sort your posts by modified

You can try something like the following: (NOTE: The following is untested and needs PHP 5.4+)

$args = [
    'orderby'        => 'modified',
    'category__in'   => [3, 7],
    'posts_per_page' => -1,
    'date_query'     => [
        [
           'column' => 'post_modified_gmt',
           'after'  => '2 days ago',
        ]
    ],
];

$q = new WP_Query( $args );

Your challenge will be to correctly display your date headers. We will need to helper function to compare the dates and return the difference. All the code goes into functions.php

/** 
 * function to return the difference as follow
 * Today will be Today's
 * Yesterday will be Yesterday's
 * Any other day will be x Days ago
 *
 * @param $start The start date in unix timestamp to compare
 * @param $end   The end date in unix timestamp to compare
 * @return (string) $days
 */
function get_relative_date_diff( $start, $end )
{
    // Make sure we have date values, if not, return false
    if ( !$start || !$end )
        return false;

    $diff        = (int) abs( $start - $end );
    $number_days = round( $diff / DAY_IN_SECONDS );

    // Output the correct string according to $day
    switch ( $number_days ) {
        case '0':
            $days = "Today's";
            break;
        case '1':
            $days = "Yesterday's";
            break;
        default:
            $days = "$number_days Days ago";
    }

    return $days;
} 

Now that that is all setup, we can now modify our loop accordingly. We will use the loop above

if ( $q->have_posts() ) {
    // Set a variable to hold the current day number
    $day_variable = '';
    // Get the current date
    $now_date = time(); 

    echo '<ul>';

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

            $post_date_day = get_the_modified_date( 'd' );
            if (    !$day_variable // Check if $day_variable is empty
                 || $day_variable != $post_date_day // Check if $day_variable is not the same as $post_date_day
            ) {
                // We are now going to output our header
                $diff         = get_relative_date_diff( $now_date, get_the_modified_date( 'U' ) );
                echo '<span>' . $diff . ' Update ' . get_the_date( 'F j, Y' ) . '</span>';
            }
            // Set the $day_variable variable to the $post_date_day variable
            $day_variable = $post_date_day;
            echo '<li><a href="'.get_permalink().'">' . get_the_title() . '</a></li>';
        }

    echo '</ul>';

    wp_reset_postdata();
}

A final note, all this code will only work correctly if the post dates are less than a month old.

EDIT

My code had a couple of flaws

  • My query arguments missed 'column' => 'post_modified_gmt', and 'posts_per_page' => -1

  • When I calculated the time difference, I used the actual post date (get_the_date), and not the date on which the post was modified on (get_the_modified_date( 'U' )). This is what caused the funny days output in the headers

  • @birgire reminded me of the human_time_diff() function. I did not use that function as you need other output from the output from human_time_diff(). I have however rewritten my function with some of the logic used in human_time_diff().

CONCLUSION

I have not fully tested the code, but on initial test it seems to do the job. You might need one or more small adjustments to your needs, so feel free to modify my code as needed. If anything is unclear, please feel free to leave a comment

11
  • I wonder why you didn't use human_time_diff() here instead of get_relative_date_diff() ?
    – birgire
    Commented Sep 27, 2015 at 19:47
  • @birgire to be really honest, I totally forgot about that ;-) Commented Sep 28, 2015 at 3:37
  • @birgire human_time_diff() is a option, yes. The output desired by the OP is slightly different, so I guess one can use the human_time_diff filter to manipulate the output. Will have a look at this later today or tommorow :-) Commented Sep 28, 2015 at 4:24
  • 1
    but anyway it's a good exercise to write such a custom function ;-)
    – birgire
    Commented Sep 28, 2015 at 8:03
  • @birgire hello sir thank you so much for the effort. But this only output what posted on that day not what posted on that day that are modified. I tried adding 'column'=> 'post_modified_gmt', and the output are 17days,21days,16days ago and kinda messy. Btw, It only output the yesterday post with single post. Commented Sep 28, 2015 at 9:05

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.