Can wp-cli display all users and their roles across all sites in a multisite instance?
-
24-02-2021 - |
Question
List Multisite Users
Using wp-cli(1) If I run wp user list
I get a list of the users (2 admins in my case) at the top level of multisite(2).
List Multisite Sites' Users
I have 25 sites with various users. Some users are in different sites as different roles.
When I use the network flag wp user list --network
I get the same style report minus the roles column; but the report spans the multisite network of sites.
List Multisite Sites' Users+Roles
My goal is to list all the admins of each site. This is where I am stuck. Apparently, adding the additional flag --role=administrator
adds nothing to the conversation. It will run the same network report of all registered users.
Is there a combination of flags that will list the users along with their roles?
References:
- wp-cli.org
- codex.wordpress.org/Glossary#Multisite
Side note: Dividing these up by site would also be handy.
La solution
List all the admins of each site
Side note: Dividing these up by site would also be handy.
Let's do that here:
We can list the url of each site with:
wp site list --field=url
We can list all administrators of a given site with:
wp user list --role=administrator --url=https://example.tld/site1
These commands can be combined with e.g. xargs
as suggested on the wp-cli site:
wp site list --field=url \
| xargs -I % wp user list --role=administrator --url=%
We might also want to use | tail --lines=+2
to skip the url header, of the first command as explained in the tail manual and answers like this one.
Thanks to @grappler for suggesting --field="url"
instead of --fields="url"
with | tail --lines=+2
to skip the header row.
If we want to display the url before each table:
for url in $( wp site list --field="url" ); \
do \
echo $url; \
wp user list --role=administrator --url=$url; \
done;