WP-cli run command over each subsite

If you use WP-cli command on a multisite it be default will only run on the mainsite.
But often you want to change a setting for all the sites.
In my case I wanted to set the timezone to Amsterdam for the whole network. That’s not hard:

wp option set timezone_string 'Europe/Amsterdam'

On a multisite this is a bit more difficult. But the script below will do the same for each site in a multisite.

wp site list --field=url | xargs -I % sh -c 'printf "%\n"; wp option set timezone_string 'Europe/Amsterdam' --url=%'

wp cli output format with –porcelain

wp db export

Will output something like Succes: Exported to 'dbname-2018-09-16-2790c11.sql'
Often you need the filename.

--porcelain flag to the rescue!

wp db export --porcelain
dbname-2018-09-16-2790c11.sql

This will work on a lot of commands that have one item output.
Stuff like wp post create ....
Here is a complete list of commands that have the –porcelain flag

Commands that output more items usually have a --format flag to handle output.

wp-cli eval-file

wp-cli can be used to execute a php file with the eval-file command.
Biggest plus; WordPress is loaded when you execute that file.
The point it will call this file is after the init action and after the later wp_loaded action.

Wp-cli autocomplete easy installed

Installing wp-cli with 2 command:

#!/usr/bin/env bash
wget https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash -O ~/wp-completion.bash -q
echo "source $HOME/wp-completion.bash" >> ~/.bashrc

And logout then login again for it to take effect.