From 6db02371ec0ea55684496eea352cfa63c7d9740e Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 2 Jun 2015 13:30:26 +0000 Subject: [PATCH] Introduce `'has_published_posts'` parameter for `WP_User_Query`. This allows user query results to be limited to those users who have published posts in at least one of the specified post types. Props joehoyle, boonebgorges. Fixes #32250. Built from https://develop.svn.wordpress.org/trunk@32683 git-svn-id: http://core.svn.wordpress.org/trunk@32653 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/user.php | 21 ++++++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/wp-includes/user.php b/wp-includes/user.php index c8d756f798..e3a1ba6108 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -569,6 +569,9 @@ class WP_User_Query { * 'url', 'registered'. Use 'all' for all fields and 'all_with_meta' to * include meta fields. Default 'all'. * @type string $who Type of users to query. Accepts 'authors'. Default empty (all users). + * @type bool|array $has_published_posts Pass an array of post types to filter results to users who have + * published posts in those post types. `true` is an alias for all + * public post types. * } */ public function prepare_query( $query = array() ) { @@ -592,7 +595,8 @@ class WP_User_Query { 'number' => '', 'count_total' => true, 'fields' => 'all', - 'who' => '' + 'who' => '', + 'has_published_posts' => null, ) ); } @@ -651,6 +655,21 @@ class WP_User_Query { $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query } + if ( $qv['has_published_posts'] && $blog_id ) { + if ( true === $qv['has_published_posts'] ) { + $post_types = get_post_types( array( 'public' => true ) ); + } else { + $post_types = (array) $qv['has_published_posts']; + } + + foreach ( $post_types as &$post_type ) { + $post_type = $wpdb->prepare( '%s', $post_type ); + } + + $posts_table = $wpdb->get_blog_prefix( $blog_id ) . 'posts'; + $this->query_where .= " AND $wpdb->users.ID IN ( SELECT DISTINCT $posts_table.post_author FROM $posts_table WHERE $posts_table.post_status = 'publish' AND $posts_table.post_type IN ( " . join( ", ", $post_types ) . " ) )"; + } + // Meta query. $this->meta_query = new WP_Meta_Query(); $this->meta_query->parse_query_vars( $qv ); diff --git a/wp-includes/version.php b/wp-includes/version.php index dd8da96768..8ece8dd50a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.3-alpha-32682'; +$wp_version = '4.3-alpha-32683'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.