Make WordPress Core

Opened 22 months ago

Closed 20 months ago

Last modified 10 months ago

#55594 closed enhancement (fixed)

Prime user meta in WP_User_Query

Reported by: spacedmonkey's profile spacedmonkey Owned by: spacedmonkey's profile spacedmonkey
Milestone: 6.1 Priority: normal
Severity: normal Version: 3.0
Component: Users Keywords: has-patch has-unit-tests commit
Focuses: performance Cc:

Description

Ensure that user meta cache is primed in the WP_User_Query class, as user meta is used heavily in WP_User class. Prime all user meta in one query will result in less queries overall.

Change History (19)

This ticket was mentioned in PR #2614 on WordPress/wordpress-develop by spacedmonkey.


22 months ago
#1

  • Keywords has-patch added; needs-patch removed

#2 @spacedmonkey
22 months ago

  • Milestone changed from Awaiting Review to 6.1
  • Owner set to spacedmonkey
  • Status changed from new to assigned
  • Version set to 3.0

#3 @spacedmonkey
22 months ago

#55621 was marked as a duplicate.

TimothyBJacobs commented on PR #2614:


21 months ago
#5

I'm not entirely following the patch here. Can you expand on the what the actual changes are here and their purpose? At first glance, it looks to be collapsing all and all_with_meta to be equivalent. Is that correct?

spacedmonkey commented on PR #2614:


21 months ago
#6

@TimothyBJacobs In WordPress 4.9, the WP_User class was changed so needs user meta. See
https://github.com/WordPress/wordpress-develop/blob/4e830858061dbe37cda462e9a861bc7401fc6f6b/src/wp-includes/class-wp-user.php#L890-L906

So in the user REST API controller and is all other places that load a list of users, user meta is not primed correctly.

TimothyBJacobs commented on PR #2614:


21 months ago
#7

So should we update the docs for WP_User_Query to indicate that these function identically?

peterwilsoncc commented on PR #2614:


21 months ago
#8

Thanks for explaining this, I was a little confused too. I can see why this is needed for both now with the changes to WP_User.

The all_with_meta option was effectively soft deprecated in 4.9 once all started populating the meta data. It could be simpler to document & throw a notice for the soft deprecation and convert it to all.

spacedmonkey commented on PR #2614:


21 months ago
#9

The all_with_meta option was effectively soft deprecated in 4.9 once all started populating the meta data. It could be simpler to document & throw a notice for the soft deprecation and convert it to all.

I have added a comment, to deprecate. I don't think we should hard deprecate, this param is used in a number of places in core and I guess in lots plugins as well.

#10 @peterwilsoncc
20 months ago

  • Keywords has-unit-tests commit added

The linked pull request looks good to me as of cd881991e.

Marking as commit ready.

#11 @spacedmonkey
20 months ago

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

In 53655:

Users: Prime user meta in WP_User_Query class.

When querying 'fields' equal to 'all' using the WP_User_Query class, this returns an array of WP_User objects. A WP_User object requires user meta to be primed, as the user's role is stored in user meta. Ensure that all users meta is primed in a single request by calling the cache_users function when querying 'fields' equal to 'all'. Soft deprecate fields equal to all_with_meta as it now acts the same as 'fields' equal to 'all'.

Props Spacedmonkey, peterwilsoncc, mehulkaklotar, timothyblynjacobs, furi3r.
Fixes #55594.

#13 @desrosj
18 months ago

  • Keywords needs-dev-note added

There are a lot of caching related changes in 6.1 and a dev note should be written collecting them all. Marking this for inclusion.

#15 follow-up: @desrosj
16 months ago

Thanks @obenland! I've opened #56988 to investigate any needed changes for 6.1.1.

@spacedmonkey was this ever included in a dev note? This one is still tagged needs-dev-note.

#16 in reply to: ↑ 15 @pbiron
16 months ago

Replying to desrosj:

@spacedmonkey was this ever included in a dev note? This one is still tagged needs-dev-note.

It's mentioned at the very bottom of https://make.wordpress.org/core/2022/10/11/performance-field-guide-for-wordpress-6-1/, but no details are provided

#17 follow-up: @spacedmonkey
16 months ago

  • Keywords needs-dev-note removed

@pbiron @desrosj I did original want to write about it. But I couldn't think of anything more than, we prime user meta in WP_User_Query now. I don't think it needs a dev note really. I think we are good here.

#18 in reply to: ↑ 17 @pbiron
16 months ago

Replying to spacedmonkey:

@pbiron @desrosj I did original want to write about it. But I couldn't think of anything more than, we prime user meta in WP_User_Query now. I don't think it needs a dev note really. I think we are good here.

Agreed...there's not much to say about it :-)

#19 @spacedmonkey
10 months ago

#36508 was marked as a duplicate.

Note: See TracTickets for help on using tickets.