When might you need to specify the “url” argument/option in WP-CLI?

Lets say I have a single multisite install on my server, and in that multisite/network admin I have 3 blogs:

  • example.com ( root/primary/first site )
  • example.com/second
  • othersite.net

If I want to list posts on the main site example.com I can do this:

wp post list

But what if I want to list posts on the example.com/second blog? Or othersite.net blog? How would it know that’s the site I wanted, not the root site? How does it know to load example.com/second or othersite.net‘s theme/plugins/etc?

Normally WordPress knows this because it’s in the HTTP headers, but there are non in WP CLI. Instead I can do this by passing --url.

wp post list --url="https://othersite.net"

Now WP CLI executes the command, and WP is loaded, but it loads the plugins and theme of othersite.net, with the posts etc of othersite.net.

My understanding of WP-CLI is that the site @aliases are the best way to target a site when you have multiple sites…

When you have multiple installs or servers. If I have 2 multisites then 2 alias’ are best, but that an alias doesn’t help me differentiate between blogs/sites in an install of WordPress.

For example, I can’t run a command on server A that executes on server B using --url that command would still be handled by that WP install even if that WP install isn’t set up to handle that URL.

For example, on my server hosting my multisite website:

❯ wp option get siteurl
https://tomjn.com
❯ wp option get siteurl --url="http://frontenberg.tomjn.com"
https://frontenberg.tomjn.com
❯ wp option get siteurl --url="http://madeupsite.com"
Error: Site 'madeupsite.com' not found. Verify `--url=<url>` matches an existing site.

I can however use an alias to run WP CLI on my local machine, and have it automatically connect to a server and use a WordPress installation in a known location, as well as specifying certain things in advance to save time. E.g. I might use wp locally, but also have wp @dev and wp @staging and wp @production.

I might also use alias’ for the same site/server but specify different options such as the current user etc. Note that alias’ aren’t used by everybody.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)