Make WordPress Core

Opened 8 years ago

Last modified 7 years ago

#38851 new defect (bug)

WP_User_Query cannot retrieve multisite users who have not been assigned a role on a site

Reported by: robdxw's profile robdxw Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Users Keywords: needs-patch needs-unit-tests
Focuses: Cc:

Description

If you add a user to a multisite at network level, but do not proceed to assign them a role on a site within the network, WP_User_Query cannot retrieve those users (even if the query is run at network level), as they have no associated wp_capabilities meta_key.

To reproduce the problem:

1) Add a user to a multisite via Network Admin > Users > Add New.
2) Run a WP_User_Query at network level (e.g. by doing something like:

add_action('load-users.php', 'myAction');

function myAction()
{
  $screen = get_current_screen();
  if( $screen->base === 'users-network' {
    $query = new WP_User_Query();
    $users = $query->results;
  }
}

The returned results will not include the user added in step 1. In fact, it will only return users who have capabilities set on the first site in the network, even though this query is not occurring at site level.

This could be fixed by allowing something like 'blog_id' => 'all' in a WP_User_Query.

Change History (9)

This ticket was mentioned in Slack in #core by helen. View the logs.


8 years ago

#2 @johnbillion
8 years ago

  • Keywords needs-patch needs-unit-tests added
  • Version trunk deleted

#3 @johnbillion
8 years ago

  • Milestone changed from Awaiting Review to 4.8

Thank you for the report, @robdxw, and welcome to Trac. I'll be tackling several issues related to users with no role in 4.8.

#4 @flixos90
8 years ago

Related: #36196

#5 @stormrockwell
8 years ago

I feel that 'blog_id' => -1 would be a better choice so we don't mix primitives and it would resemble 'all' like 'posts_per_page' => -1 in many WP functions.

This ticket was mentioned in Slack in #core by flixos90. View the logs.


7 years ago

#7 @flixos90
7 years ago

  • Milestone changed from 4.8 to Future Release

Let's deal with this in a future release, in combination with the other tickets related to no-role users.

#8 @tomdxw
7 years ago

It's actually possible to retrieve all users on multisite: get_users(['blog_id' => 0]);

This is counterintuitive because in most of WordPress, -1 is used to indicate "all" and 0 is used to indicate "use the current site or user". And this feature appears to be undocumented. The documentation just says "(int) The site ID. Default is the current site."

I'm not going to mark this bug as fixed. I think this feature should be documented, and 'blog_id' => -1 should work too, for consistency.

#9 @demsei
7 years ago

Good catch @robdxw! I am interested in a fix for this as well. For now I am using the solution @tomdxw posted:

<?php
$args = array(
  'blog_id' => 0,
  'meta_query' => array(
    array(
      'key' => 'my.special.key',
      'compare' => 'NOT EXISTS'
    )
  )
);
Note: See TracTickets for help on using tickets.