WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 4 years ago

#32250 closed enhancement (fixed)

Ability to get_users() who have published posts

Reported by: joehoyle Owned by: joehoyle
Milestone: 4.3 Priority: normal
Severity: normal Version:
Component: Query Keywords: has-patch rest-api needs-docs
Focuses: Cc:
PR Number:

Description

Currently there is no easy way to get a list of users who are "public" on the site. A user is typically public if they have a published viewable post, as their user is associated with and displayed on that post (both in the theme and in the feeds).

In the REST API, we ideally want to show publicly available users at /wp/users for unauthenticated requests. Currently WP_User_Query supports who = authors which _actually_ returns a list of users who are user level 1 or above. In this case, not much use. We already support GET /wp/users/123 for a user who say published posts.

I propose another argument be supported for WP_User_Query to get said public authors of the site. I put together an initially patch which adds who = public_authors (though I frankly find "who" to be a super weird keyword!).

This will basically join the posts table in the query so select anyone who has a published post of a public post type.

Attachments (4)

32250.diff (2.7 KB) - added by joehoyle 5 years ago.
32250.1.diff (2.7 KB) - added by joehoyle 5 years ago.
Use a subquery for posts table
32250-2.diff (4.4 KB) - added by joehoyle 4 years ago.
has_published_posts option
32250.2.diff (6.1 KB) - added by boonebgorges 4 years ago.

Download all attachments as: .zip

Change History (23)

@joehoyle
5 years ago

#1 @wonderboymusic
5 years ago

Can't join across global tables (something about sharding or .com), would probably have to be sub-select

#2 @joehoyle
5 years ago

Ah, my bad. I can update - however, interested if this is a wanted patch or not.

#3 @johnbillion
5 years ago

  • Keywords needs-patch added

#4 @rmccue
5 years ago

  • Milestone changed from Awaiting Review to 4.3
  • Owner set to joehoyle
  • Status changed from new to assigned

This is one we need for the REST API, so let's get it in ASAP.

This ticket was mentioned in Slack in #core-restapi by joehoyle. View the logs.


5 years ago

@joehoyle
5 years ago

Use a subquery for posts table

#6 @joehoyle
5 years ago

  • Keywords has-patch added; needs-patch removed

#7 @boonebgorges
5 years ago

Related API repo tickets: https://github.com/WP-API/WP-API/issues/839, https://github.com/WP-API/WP-API/issues/297

It would be nice to avoid using 'who', which is crusty both in conception and in execution #15871. 'who' says "role" to me, and what we're looking for here is not directly related to roles. An argument like 'has_public_posts' => true is better, but still very specific. 'has_public_posts' => get_post_types( array( 'public' => true ) ) is better still.

It's a lot more work, but something like this would be of much broader use:

'post_count' => array(
    'operator' => '>=', // Default to '='?
    'post_types' => $post_types, // Default to array( 'post' )? Or all public types?
    'post_status' => $post_statuses, // Default to array( 'publish' ),
),

Obviously I don't want to overengineer this, but at least a 'post_count' argument feels like something that would be useful beyond just the specific needs of the API. Maybe the 'has_public_posts' for now?

#8 @joehoyle
4 years ago

I'm +1 on not using who - 'has_public_posts' => get_post_types( array( 'public' => true ) ) seems somewhat of an anti-pattern - 'has_published_posts' => get_post_types( array( 'public' => true ) ) seems more aptly named. Or has_published_posts_of_type would be more descriptive.

I like the post_count idea - however I'm not sure if that's half way to a real query API for complex user querying based off post selection. I don't know if the end goal could be use WP_Query ala WP_Meta_Query to mixin the query with the user query.

I'd say for now, has_published_posts with an array of any post types would be generic enough, without creating a another pseudo posts query language separate to WP_Query.

#9 @boonebgorges
4 years ago

  • Keywords needs-patch needs-unit-tests added; has-patch removed

"Anti-pattern" seems a bit harsh as a criticism of 'has_public_posts' vs 'has_published_posts' :) Either param name seems fine to me.

Or has_published_posts_of_type would be more descriptive.

I'd suggest that the parameter be called 'has_published_posts'. Accepts the following values:

  • null, which is the default. When null, ignore the param.
  • true When true, assume all public post types.
  • false When false, assume all public post types, but do a NOT IN
  • an array of post types

#10 @joehoyle
4 years ago

Apologies, I didn't mean anitipattern in a negative way - the point was that has_public_posts I thought sounds like "has posts in a public post type" rather than "has public posts (aka published) in a post type.

Happy to write the patch for the above proposal.

@joehoyle
4 years ago

has_published_posts option

#11 @joehoyle
4 years ago

@boonebgorges I had a bash at a patch - let me know what you think!

#12 @joehoyle
4 years ago

(also, not sure if I'm meant to change the "needs-patch" "needs-unit-tests" myself, or if that should wait until the patch has been reviewed)

#13 @boonebgorges
4 years ago

  • Keywords has-patch added; needs-patch needs-unit-tests removed

Thanks for the updated patch, joehoyle!

I think you misread my meaning for false. I meant that it should return users who do not have any published posts in public post types; you interpreted that it should return users who have published posts in non-public post types. Either way, the more I think about it, the less a false value makes sense. Let's leave it out. true will simply be shorthand for all public post types.

I'm going to clean up a number of items (better escaping, proper support for $blog_id, slightly more structured unit tests) and post another patch here. Before committing, I'd like to ask someone from the WP-API team to double-check to be sure that the new parameter will do what you need it to do.

@boonebgorges
4 years ago

#14 @joehoyle
4 years ago

Sounds good, thanks for the clean up! I'm on the WP API team, but if you want to see the ticket we have to track this it's here https://github.com/WP-API/WP-API/issues/839

This ticket was mentioned in Slack in #core-restapi by joehoyle. View the logs.


4 years ago

#16 @rmccue
4 years ago

  • Keywords rest-api added

#17 @boonebgorges
4 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 32683:

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.

#18 @DrewAPicture
4 years ago

  • Keywords needs-docs added
  • Resolution fixed deleted
  • Status changed from closed to reopened

We'll also need a changelog entry in the DocBlock for the new parameter, please :)

#19 @boonebgorges
4 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 32685:

Add @since entry for 'has_published_post' argument of WP_User_Query.

Mega-props DrewAPicture.
Fixes #32250.

Note: See TracTickets for help on using tickets.