Opened 10 years ago
Closed 10 years ago
#32250 closed enhancement (fixed)
Ability to get_users() who have published posts
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 4.3 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Query | Keywords: | has-patch rest-api needs-docs |
Focuses: | Cc: |
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)
Change History (23)
#4
@
10 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.
10 years ago
#7
@
10 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
@
10 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
@
10 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. Whennull
, ignore the param.true
Whentrue
, assume all public post types.false
Whenfalse
, assume all public post types, but do aNOT IN
- an array of post types
#10
@
10 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.
#12
@
10 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
@
10 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.
#14
@
10 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
Can't join across global tables (something about sharding or .com), would probably have to be sub-select