diff --git a/src/wp-admin/includes/dashboard.php b/src/wp-admin/includes/dashboard.php index 778e3de40326b..93d89ad3addf5 100644 --- a/src/wp-admin/includes/dashboard.php +++ b/src/wp-admin/includes/dashboard.php @@ -1015,7 +1015,7 @@ function wp_dashboard_recent_posts( $args ) { $today = current_time( 'Y-m-d' ); $tomorrow = current_datetime()->modify( '+1 day' )->format( 'Y-m-d' ); - $year = current_time( 'Y' ); + $year = (int) current_time( 'Y' ); while ( $posts->have_posts() ) { $posts->the_post(); @@ -1053,6 +1053,106 @@ function wp_dashboard_recent_posts( $args ) { } echo ''; + + /* + * List posts published on this day in previous years if the final post + * in the recently published list is within the past year (minus 1 day). + */ + if ( 'published-posts' === $args['id'] && is_int( $time ) + && $time > ( current_time( 'U' ) - YEAR_IN_SECONDS + ( 1 * DAY_IN_SECONDS ) ) ) { + + $wp_otd_date = current_datetime(); + $wp_otd_clauses = array(); + $window_before_days = 0; // Edit before and after if creating a range. + $window_after_days = 0; + $user_id = get_current_user_id(); + + for ( $offset = -$window_before_days; $offset <= $window_after_days; $offset++ ) { + $wp_otd_day_date = $wp_otd_date->modify( ( $offset >= 0 ? '+' : '' ) . $offset . ' days' ); + $wp_otd_clauses[] = array( + 'month' => (int) $wp_otd_day_date->format( 'n' ), + 'day' => (int) $wp_otd_day_date->format( 'j' ), + ); + } + + $wp_otd_date_query = array( + 'relation' => 'AND', + array( + 'before' => array( 'year' => $year ), + ), + array_merge( + array( 'relation' => 'OR' ), + $wp_otd_clauses + ), + ); + + $wp_otd_args = array( + 'author' => (int) $user_id, + 'post_type' => 'post', + 'post_status' => array( 'publish' ), + 'posts_per_page' => 50, + 'ignore_sticky_posts' => true, + 'orderby' => 'date', + 'order' => 'DESC', + 'no_found_rows' => true, + 'update_post_term_cache' => false, + 'date_query' => $wp_otd_date_query, + ); + + /** + * Filters the arguments used to query posts for the On This Day dashboard widget. + * + * @since 7.1.0 + * + * @param array $wp_otd_args WP_Query arguments. + * @param int $user_id The author ID the query is scoped to. + */ + $wp_otd_args = apply_filters( 'wp_dashboard_on_this_day_query_args', $wp_otd_args, $user_id ); + + $wp_otd_query = new WP_Query( $wp_otd_args ); + + if ( $wp_otd_query->have_posts() ) { + + echo '