Make WordPress Core


Ignore:
Timestamp:
06/02/2015 01:29:44 PM (11 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r32637 r32683  
    570570     *                                         include meta fields. Default 'all'.
    571571     *     @type string       $who             Type of users to query. Accepts 'authors'. Default empty (all users).
     572     *     @type bool|array   $has_published_posts Pass an array of post types to filter results to users who have
     573     *                                             published posts in those post types. `true` is an alias for all
     574     *                                             public post types.
    572575     * }
    573576     */
     
    593596                'count_total' => true,
    594597                'fields' => 'all',
    595                 'who' => ''
     598                'who' => '',
     599                'has_published_posts' => null,
    596600            ) );
    597601        }
     
    650654            $qv['meta_compare'] = '!=';
    651655            $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
     656        }
     657
     658        if ( $qv['has_published_posts'] && $blog_id ) {
     659            if ( true === $qv['has_published_posts'] ) {
     660                $post_types = get_post_types( array( 'public' => true ) );
     661            } else {
     662                $post_types = (array) $qv['has_published_posts'];
     663            }
     664
     665            foreach ( $post_types as &$post_type ) {
     666                $post_type = $wpdb->prepare( '%s', $post_type );
     667            }
     668
     669            $posts_table = $wpdb->get_blog_prefix( $blog_id ) . 'posts';
     670            $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 ) . " ) )";
    652671        }
    653672
Note: See TracChangeset for help on using the changeset viewer.