diff --git a/src/wp-includes/date.php b/src/wp-includes/date.php index 2634f2a7a0..ea2b4a60a2 100644 --- a/src/wp-includes/date.php +++ b/src/wp-includes/date.php @@ -451,7 +451,8 @@ class WP_Date_Query { $valid_columns = array( 'post_date', 'post_date_gmt', 'post_modified', - 'post_modified_gmt', 'comment_date', 'comment_date_gmt' + 'post_modified_gmt', 'comment_date', 'comment_date_gmt', + 'user_registered', ); // Attempt to detect a table prefix. @@ -463,7 +464,8 @@ class WP_Date_Query { * * @param array $valid_columns An array of valid date query columns. Defaults * are 'post_date', 'post_date_gmt', 'post_modified', - * 'post_modified_gmt', 'comment_date', 'comment_date_gmt' + * 'post_modified_gmt', 'comment_date', 'comment_date_gmt', + * 'user_registered' */ if ( ! in_array( $column, apply_filters( 'date_query_valid_columns', $valid_columns ) ) ) { $column = 'post_date'; @@ -480,6 +482,9 @@ class WP_Date_Query { 'comment_date', 'comment_date_gmt', ), + $wpdb->users => array( + 'user_registered', + ), ); // If it's a known column name, add the appropriate table prefix. diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index b5f05935bc..ed3f4af6ea 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -742,6 +742,12 @@ class WP_User_Query { $this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)"; } + // Date queries are allowed for the user_registered field. + if ( ! empty( $qv['date_query'] ) && is_array( $qv['date_query'] ) ) { + $date_query = new WP_Date_Query( $qv['date_query'], 'user_registered' ); + $this->query_where .= $date_query->get_sql(); + } + /** * Fires after the WP_User_Query has been parsed, and before * the query is executed. diff --git a/tests/phpunit/tests/user/dateQuery.php b/tests/phpunit/tests/user/dateQuery.php new file mode 100644 index 0000000000..3add9507c3 --- /dev/null +++ b/tests/phpunit/tests/user/dateQuery.php @@ -0,0 +1,58 @@ +factory->user->create( array( + 'user_registered' => '2012-02-14 05:05:05', + ) ); + $u2 = $this->factory->user->create( array( + 'user_registered' => '2013-02-14 05:05:05', + ) ); + + $uq = new WP_User_Query( array( + 'date_query' => array( + array( + 'year' => 2012, + ), + ), + ) ); + + $this->assertEqualSets( array( $u1 ), wp_list_pluck( $uq->results, 'ID' ) ); + } + + /** + * @ticket 27283 + */ + public function test_user_registered_relation_or() { + $u1 = $this->factory->user->create( array( + 'user_registered' => '2012-02-14 05:05:05', + ) ); + $u2 = $this->factory->user->create( array( + 'user_registered' => '2013-02-14 05:05:05', + ) ); + $u3 = $this->factory->user->create( array( + 'user_registered' => '2014-02-14 05:05:05', + ) ); + + $uq = new WP_User_Query( array( + 'date_query' => array( + 'relation' => 'OR', + array( + 'year' => 2013, + ), + array( + 'before' => '2012-03-01 00:00:00', + ), + ), + ) ); + + $this->assertEqualSets( array( $u1, $u2 ), wp_list_pluck( $uq->results, 'ID' ) ); + } +}