Skip to content

Commit aa09b83

Browse files
committed
Switch to WP_Query
1 parent eceeec1 commit aa09b83

1 file changed

Lines changed: 19 additions & 31 deletions

File tree

src/wp-admin/includes/class-wp-on-this-day.php

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class WP_On_This_Day {
4040
* @since 7.1.0
4141
* @var int
4242
*/
43-
const CACHE_VERSION = 5;
43+
const CACHE_VERSION = 6;
4444

4545
/**
4646
* Renders the dashboard widget output.
@@ -98,12 +98,20 @@ public static function render_dashboard_widget() {
9898
* Retrieves posts by a given author that were published on this
9999
* same month and day in previous years.
100100
*
101+
* The "same month/day, prior year" constraint is expressed as a
102+
* `date_query`: a clause pinning `month`/`day`, combined with a
103+
* `before` clause anchored to January 1 of the current year.
104+
*
101105
* @since 7.1.0
102106
*
103107
* @param int $user_id Author ID to query posts for.
104108
* @return WP_Post[] Array of posts ordered by newest first.
105109
*/
106110
public static function get_posts( $user_id ) {
111+
$month = (int) current_time( 'n' );
112+
$day = (int) current_time( 'j' );
113+
$year = (int) current_time( 'Y' );
114+
107115
$args = array(
108116
'author' => (int) $user_id,
109117
'post_type' => 'post',
@@ -113,6 +121,15 @@ public static function get_posts( $user_id ) {
113121
'orderby' => 'date',
114122
'order' => 'DESC',
115123
'no_found_rows' => true,
124+
'date_query' => array(
125+
array(
126+
'month' => $month,
127+
'day' => $day,
128+
),
129+
array(
130+
'before' => array( 'year' => $year ),
131+
),
132+
),
116133
);
117134

118135
/**
@@ -125,40 +142,11 @@ public static function get_posts( $user_id ) {
125142
*/
126143
$args = apply_filters( 'dashboard_on_this_day_query_args', $args, $user_id );
127144

128-
add_filter( 'posts_where', array( __CLASS__, 'filter_posts_where' ) );
129145
$query = new WP_Query( $args );
130-
remove_filter( 'posts_where', array( __CLASS__, 'filter_posts_where' ) );
131146

132147
return $query->posts;
133148
}
134149

135-
/**
136-
* Restricts the widget's query to the current month and day in prior years.
137-
*
138-
* @since 7.1.0
139-
*
140-
* @global wpdb $wpdb WordPress database abstraction object.
141-
*
142-
* @param string $where SQL WHERE clause.
143-
* @return string Filtered WHERE clause.
144-
*/
145-
public static function filter_posts_where( $where ) {
146-
global $wpdb;
147-
148-
$month = (int) current_time( 'n' );
149-
$day = (int) current_time( 'j' );
150-
$year = (int) current_time( 'Y' );
151-
152-
$where .= $wpdb->prepare(
153-
" AND MONTH({$wpdb->posts}.post_date) = %d AND DAY({$wpdb->posts}.post_date) = %d AND YEAR({$wpdb->posts}.post_date) < %d",
154-
$month,
155-
$day,
156-
$year
157-
);
158-
159-
return $where;
160-
}
161-
162150
/**
163151
* Renders the empty state shown when no matching posts exist.
164152
*
@@ -269,7 +257,7 @@ protected static function render_post( $post ) {
269257
$excerpt = wp_trim_words( trim( $excerpt ), 24, '&hellip;' );
270258

271259
$time_str = get_the_time( get_option( 'time_format' ), $post );
272-
$time_iso = get_the_time( 'Y-m-d H:i', $post );
260+
$time_iso = get_the_time( 'c', $post );
273261
$categories = get_the_category( $post->ID );
274262

275263
$row_classes = 'on-this-day-post';

0 commit comments

Comments
 (0)