Make WordPress Core

Opened 3 months ago

Last modified 3 months ago

#54310 new defect (bug)

WP_User_Query does not return results in the same format if fields set to all_with_meta and doesn't return any metadata

Reported by: pbearne Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Users Keywords: needs-patch needs-unit-tests
Focuses: Cc:

Description

Digging into WP_User_Query if you set the fields to all_with_meta it returns an array index by the user_ID veers a keyed list for all other results

this is the

		if ( 'all_with_meta' === $qv['fields'] ) {
			cache_users( $this->results );

			$r = array();
			foreach ( $this->results as $userid ) {
				$r[ $userid ] = new WP_User( $userid, '', $qv['blog_id'] );
			}

			$this->results = $r;
		} elseif ( 'all' === $qv['fields'] ) {
			foreach ( $this->results as $key => $user ) {
				$this->results[ $key ] = new WP_User( $user, '', $qv['blog_id'] );
			}
		}


I feel we should return the same shape array for both so propose to set the all to array index to the User_ID's

with a change like this

		if ( 'all_with_meta' === $qv['fields'] ) {
			cache_users( $this->results );

			$r = array();
			foreach ( $this->results as $userid ) {
				$r[ $userid ] = new WP_User( $userid, '', $qv['blog_id'] );
			}

			$this->results = $r;
		} elseif ( 'all' === $qv['fields'] ) {
			foreach ( $this->results as $user ) {
				$this->results[ $user->ID ] = new WP_User( $user, '', $qv['blog_id'] );
			}
		}

And better still it doesn't return the meta values !!!

So let's add this as well

		if ( 'all_with_meta' === $qv['fields'] ) {
			cache_users( $this->results );

			$r = array();
			foreach ( $this->results as $userid ) {
				$r[ $userid ] = new WP_User( $userid, '', $qv['blog_id'] );
				$r[ $userid ]->meta = get_user_meta( $userid );
			}

			$this->results = $r;
		} elseif ( 'all' === $qv['fields'] ) {
			foreach ( $this->results as $key => $user ) {
				$this->results[ $key ] = new WP_User( $user, '', $qv['blog_id'] );
			}
		}

Change History (2)

#1 @paulbigai
3 months ago

  • Summary changed from WP_User_Query does not return results in the save format if fields set to all_with_meta and doesn't return any meatadata to WP_User_Query does not return results in the save format if fields set to all_with_meta and doesn't return any metadata

#2 @pbearne
3 months ago

  • Summary changed from WP_User_Query does not return results in the save format if fields set to all_with_meta and doesn't return any metadata to WP_User_Query does not return results in the same format if fields set to all_with_meta and doesn't return any metadata
Note: See TracTickets for help on using tickets.