Run a WP-CLI command as a given WordPress user

If you like my WordPress work, check out my new plugin, Bylines. Thanks!

By default, WP-CLI executes every command as a logged-out, unauthenticated user.

$ wp eval 'echo get_current_user_id();'
0

To execute a command as an existing WordPress user, you can use the --user=<user> global parameter, which accepts a user id, login, or email address:

$ wp eval 'echo get_current_user_id();' --user=runcommand
1

It’s worth noting WP-CLI’s user handling is relatively special; even though commands are executed as a logged-out user by default, you can perform many operations that typically require authentication and authorization in the WordPress dashboard.