WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 2 years ago

#31383 new feature request

Add WP_Tax_Query support to WP_User_Query

Reported by: desrosj Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 4.2
Component: Query Keywords: needs-patch
Focuses: Cc:
PR Number:

Description

Users like any other object type in WordPress can have taxonomy terms assigned to them. However, there is currently no way to query by taxonomies in WP_User_Query.

As far as I could find, the only way to really accomplish this right now is to use get_objects_in_term(), filter out the non user objects, and then use the include parameter.

Ideally, I think you should be able to accomplish this by the following:

$user_query = new WP_User_Query( array(
	'tax_query' => array(
		array(
			'taxonomy' => 'tax_name',
			'field' => 'slug',
			'terms' => array( 'term-1', 'term-2' ),
		)
	)
) );

Attachments (2)

User WP_Tax_Query support.patch (3.4 KB) - added by desrosj 5 years ago.
First pass.
31383.diff (9.1 KB) - added by desrosj 3 years ago.
Applies WP_Tax_Query support to WP_User_Query, no UX.

Download all attachments as: .zip

Change History (16)

#1 follow-up: @boonebgorges
5 years ago

  • Milestone changed from Awaiting Review to Future Release

This would be easy to add, but I'm somewhat wary. It's possible to assign taxonomies and terms to users, but there's no explicit support for that in the system - you kinda have to hack it. Adding a 'tax_query' parameter would be like an official endorsement for user taxonomies, without actually having them fully implemented.

Maybe we can use this ticket to think about some of the core changes that would be necessary to introduce minimal support for user taxonomies. Off the top of my head:

  • When you register a taxonomy with 'user' as the object type, the taxonomy's UI should appear under the 'Users' menu in the Dashboard
  • We should examine whether we need a user-specific generic 'update_count_callback', along the lines of '_update_post_term_count()' for post types. (I think we probably would, because we'd want, eg, spammed users to be excluded from term counts.)
  • Built-in UI for managing user tax terms on profile.php (maybe?)
  • Built-in support for user taxonomy archives. So example.com/user_tax/foo would show a list of users with the 'foo' term in the 'user_tax' taxonomy. (another maybe - not sure if this is desirable in core, or necessary for a first pass)

#2 in reply to: ↑ 1 @desrosj
5 years ago

Replying to boonebgorges:

  • When you register a taxonomy with 'user' as the object type, the taxonomy's UI should appear under the 'Users' menu in the Dashboard
  • We should examine whether we need a user-specific generic 'update_count_callback', along the lines of '_update_post_term_count()' for post types. (I think we probably would, because we'd want, eg, spammed users to be excluded from term counts.)

I agree with both of these. Definitely would need this for a round one.

  • Built-in UI for managing user tax terms on profile.php (maybe?)

We would have to consider the differences between hierarchical and non-hierarchical taxonomies. Could potentially be two different interfaces like categories and tags, but I like putting it on profile.php.

Another thing to consider is additions to the users.php page.

  • Adding a column for the taxonomy based on 'show_admin_column' argument.
  • Whether to add dropdown filters.

I can take a first stab at this if you think it has potential for good direction.

#3 follow-up: @boonebgorges
5 years ago

  • Keywords needs-patch added

Cool - this sounds good, desrosj.

I'm positive that this has been mentioned in past Trac tickets, but I'm having a hard time finding it. I'd say that, before starting anything, it's a good idea to do some ticket archaeology. If past discussions have taken place on the subject of user taxonomies, it'd be good to read them over first. I know that there are also some existing plugins that add user taxonomies, so it may be a good idea to look them over - if not for the purposes of reusing code, then at least for studying the parts of the taxonomy API they've seen fit to replicate/build out for user taxonomies.

Regarding patching: let's start very modest. I think that the first two bullet points that you've outlined are probably the absolute minimum that would be required to justify a 'tax_query' param in WP_User_Query. Maybe best to start with those? The profile.php bit, theme template support, and your users.php suggestions are probably not required for a first pass, and may even be best in a plugin (especially the dropdown filters - we don't even offer this with posts). 'show_admin_column' support would be pretty nice, though, and would give good parity with post taxonomies.

#4 @desrosj
5 years ago

Great!

I did some digging too, but couldn't find anything either. I couldn't believe there wasn't a ticket for this already though, so I will keep looking.

For the dropdown filters, #23422 is relevant. If that gets added, we could revisit that aspect down the road.

#5 in reply to: ↑ 3 @SergeyBiryukov
5 years ago

Replying to boonebgorges:

I'm positive that this has been mentioned in past Trac tickets, but I'm having a hard time finding it.

Related: #23045

#6 @boonebgorges
5 years ago

#31512 was marked as a duplicate.

@desrosj
5 years ago

First pass.

#7 @desrosj
5 years ago

Patch above accomplishes the following:

  • Shows menu items for taxonomies registered to users linking to a taxonomy management screen.
  • Allows the 'tax_query' parameter to be passed in a WP_User_Query the same as WP_Query calls.
  • Parses a url with a taxonomy's query_var populated into a query (using slugs).

Still have a checklist of what remains, but wanted to post something to show some progress and get feedback. I think the actual query aspect of things is in great shape.

Remaining items:

  • Menu items for taxonomies still need to be selected when on the respective pages.
  • Taxonomy management screens need to link to the user screen instead of posts.
  • Provide a way for terms to be applied to a user.
  • A user-specific generic 'update_count_callback', along the lines of '_update_post_term_count()' for post types.
  • Show a column in the admin when show_admin_column is set to true.

Below is a quick example of what we could do on the user edit screen.
https://dl.dropboxusercontent.com/u/2200339/WordPress%20Tickets/user-taxomies.png

#8 @chrisvanpatten
3 years ago

I'm working on a project that would really benefit from being able to do tax queries on users. There are other ways (with user meta) that could accomplish the same thing, but a tax query would be much easier/more efficient/easier to grok.

Would a refreshed patch be the best way to kick this off, with the aim to get this into 4.7?

Edit: Also, what would be the minimum 'scope of work' on this ticket? Should we just aim to get tax_query in, and then leave UX concerns (taxonomies showing up in the Users menu, edit fields on the User page, etc.) to a future/separate ticket?

I can imagine user taxonomy UI/UX being useful (particularly for 'groups' and similar constructs) but it may be easier to separate that out, and just focus on the infrastructure component here.

Last edited 3 years ago by chrisvanpatten (previous) (diff)

#9 @boonebgorges
3 years ago

#37256 was marked as a duplicate.

#10 @boonebgorges
3 years ago

@desrosj Thanks for your patch on this ticket, and apologies for letting it fall through the cracks. Your patch is a useful starting point.

@chrisvanpatten asks:

Also, what would be the minimum 'scope of work' on this ticket? Should we just aim to get tax_query in, and then leave UX concerns (taxonomies showing up in the Users menu, edit fields on the User page, etc.) to a future/separate ticket?

This is a great question. I definitely think that 'tax_query' support can be worked on as a standalone patch. (More on WP_Tax_Query support.patch in a moment.) In a pinch, I think we could put 'tax_query' support into a release without any user-facing UX.

That said, I think it would make for a more compelling feature if we could flesh out some of the UX stuff as part of a single release. The operating slogan is, I think, something like this: "If my plugin registers a taxonomy with object type 'user', it should feel like a native part of WP." Obviously, there's some room for interpretation regarding what that means. But I'd say that, at a minimum, it covers the first two items I mentioned in my first comment: admin menu placement and a better fallback for 'update_count_callback'. (The latter is not strictly user-facing, but it does determine the counts that are shown to users.) These two features, plus 'tax_query' in WP_User_Query, seem like a pretty good first pass.

A good next step is a refresh of WP_Tax_Query support.patch. Here are some notes:

  • parse_tax_query() doesn't check to see whether the requested taxonomies are assigned to the 'user' object. Should it? We don't currently do this elsewhere with 'tax_query' - for example, in WP_Query we don't check to see whether the taxonomies match post_type, even though taxonomies are registered against a post_type object. But the potential for confusion rises if we're providing more "official" support for user taxonomies. Here's a way to frame the issue: are we likely to confuse developers if we allow (ie, don't throw errors for) queries like get_users( ... 'tax_query' => ... 'taxonomy=post_tag' )? Or get_posts( ... 'tax_query' => ... 'taxonomy=some_user_taxonomy' )? Or maybe these queries will just always end up empty? We should think through the possible confusions (or, maybe, lack thereof).
  • It looks like the URL parsing ($t->query_var etc) is copied from WP_Query. WP_Query is responsible for the main query, which involves parsing query vars from the URL. WP_User_Query doesn't currently do this, so I think we don't need URL support here. If we introduce user-tax archives, we might consider it - but this is a way off (we don't currently have *any* front-end listings of users built into our theme hierarchy).
  • We'll need inline docs and unit tests.

Thanks for your interest in the ticket! It would be wonderful to see the project move forward.

#11 @desrosj
3 years ago

Incoming updated patch that will apply cleanly to trunk. This patch adds taxonomy query support to user queries with no UX. It includes a few new unit tests, but since this is the first time writing unit tests, I probably missed a bunch.

While refreshing the patch, I realized that there is one main issue we will need to solve in order to make this feature work. Term relationships are based on object IDs. But currently, the object ID is always assumed to be a post. This is an issue because users can have the same ID as posts since they are stored in separate tables. Without addressing this issue, taxonomy support for users can not be added for taxonomies that are not exclusive to users.

Example: if there is a post with an ID of 3, and a user with an ID of 3, calling wp_set_object_terms() will assign the term to both, regardless of the intent.

#38265 was recently opened, and this could be one potential way to fix the issue. Not sure if there are any other ideas how to accomplish this without altering tables.

@desrosj
3 years ago

Applies WP_Tax_Query support to WP_User_Query, no UX.

#12 @Qwindoo
3 years ago

Hi,

I need taxonomy query support for user queries as well. The patch seems great, however it is not ideal because of the issue mentioned by @desrosj : terms are currently assigned to all objects and there's no non-hackish way of assigning a term to a specific object type.

The cleanest way to fix this seems to be adding a new object_type column in term_relationships, alongside object_id. This would allow us to cleanly distinguish between post-to-term, user-to-term and any other future relationships we might want to support. Functions like wp_set_object_terms could accept an additional argument $object_type, defaulting to post in order to preserve existing functionality.

With this change in place, even without adding the UX stuff, being able to assign terms to users and only users and then to query users based on these terms seems a compelling enough feature to me.

Is there a specific workflow in place to request and discuss such a database schema change?

#13 @boonebgorges
3 years ago

Hi @Qwindoo - #37686 is an existing ticket for what you've proposed.

Without addressing this issue, taxonomy support for users can not be added for taxonomies that are not exclusive to users.

Correct. You can share a taxonomy between post types, but not between object types.

@desrosj Thanks for the refreshed patch. Why the hardcoded if ( 'user' != ... checks in register_taxonomy_for_object_type()? It seems to me that the restriction described above - the fact that taxonomies cannot be shared across multiple object types - will require that we lean heavily on a taxonomy's registered object_type.

#14 @drewcovi
2 years ago

Any chance we can close on this? are we waiting on a response from @desrosj ?

Note: See TracTickets for help on using tickets.