Using WordPress ‘get_day_link()’ PHP function

The get_day_link() WordPress PHP function retrieves the permalink for the day archives with year and month.

Usage

get_day_link( $year, $month, $day );

Parameters

  • $year (int|false) – Integer of year. False for current year.
  • $month (int|false) – Integer of month. False for current month.
  • $day (int|false) – Integer of day. False for current day.

More information

See WordPress Developer Resources: get_day_link()

Examples

Retrieve the permalink for the day archive of January 1, 2023.

$day_link = get_day_link( 2023, 1, 1 );
echo $day_link; // Output: https://example.com/2023/01/01/

Display day link for a post within The Loop

Get the permalink for the day archive of a specific post and display it as a link.

$archive_year = get_the_time( 'Y' );
$archive_month = get_the_time( 'm' );
$archive_day = get_the_time( 'd' );

echo '<a href="' . esc_url( get_day_link( $archive_year, $archive_month, $archive_day ) ) . '">This day’s posts</a>';

Get the permalink for the current day’s archive and display it as a link.

echo '<a href="' . esc_url( get_day_link( false, false, false ) ) . '">Today’s posts</a>';

Retrieve the permalink for the day archive of July 4, 2022 and display it with custom text.

$day_link = get_day_link( 2022, 7, 4 );
echo '<a href="' . esc_url( $day_link ) . '">Independence Day Archive</a>';

List all posts of a specific day

Display all posts of a specific day (e.g., May 5, 2023) with their respective day archive links.

$query = new WP_Query( array(
    'year' => 2023,
    'monthnum' => 5,
    'day' => 5
) );

while ( $query->have_posts() ) {
    $query->the_post();
    echo '<a href="' . esc_url( get_day_link( 2023, 5, 5 ) ) . '">' . get_the_title() . '</a><br>';
}
wp_reset_postdata();