Using WordPress ‘is_new_day()’ PHP function

The is_new_day() WordPress PHP function determines whether the publish date of the current post in the loop is different from the publish date of the previous post in the loop.

Usage

if ( is_new_day() ) {
    // Do something if it's a new day
} else {
    // Do something else if it's not a new day
}

Parameters

  • None

More information

See WordPress Developer Resources: is_new_day()

Examples

Displaying “New Day” Heading

Display a “New Day” heading before the first post of each new day in the loop.

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();

        if ( is_new_day() ) {
            echo '<h2>New Day</h2>';
        }

        // Display the post content here
    }
}

Adding a CSS Class to Posts on a New Day

Add a special CSS class to the posts that are published on a new day.

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();

        $new_day_class = is_new_day() ? 'new-day' : '';

        echo '<div class="post ' . $new_day_class . '">';
        // Display the post content here
        echo '</div>';
    }
}

Separating Posts by Day with Horizontal Line

Separate posts in the loop with a horizontal line if they are from different days.

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();

        if ( is_new_day() ) {
            echo '<hr>';
        }

        // Display the post content here
    }
}

Displaying Date for Posts on a New Day

Display the date before the post title for posts published on a new day.

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();

        if ( is_new_day() ) {
            echo '<p class="post-date">' . get_the_date() . '</p>';
        }

        // Display the post title and content here
    }
}

Counting Posts Per Day

Count the number of posts published each day in the loop.

$day_count = 0;

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();

        if ( is_new_day() ) {
            if ( $day_count > 0 ) {
                echo '<p>Posts today: ' . $day_count . '</p>';
            }

            $day_count = 1;
        } else {
            $day_count++;
        }

        // Display the post content here
    }

    // Display the post count for the last day in the loop
    echo '<p>Posts today: ' . $day_count . '</p>';
}