I know I can get a site list with wp site list --path="$pathtowordpress"

What I want to do though is get one site's ID by knowing only it's url.

Does anyone know if this is possible ?



It looks like --url isn't working to filter the wp site list output.

So instead one could try:

wp site list | awk '{ if( $2 == SITE_URL_STRING ) print $1; }'

where we use the awk trick from here, to filter the url column and display the blog_id column.

Here we must replace SITE_URL_STRING with e.g. "https://blog.example.com/site9/".

Update: Here's a bash example to find the exact site url string:

wp site list | awk -v site_url=$site_url '{ if( $2 == site_url ) print $1; }'

where we use the -v option to pass a shell variable to awk. Got that idea from here.

| improve this answer | |
  • EXCELLENT ! - That solved the problem. I adjusted that by just a bit to remove the == requirement as I wanted to rule out http vs https requirements. This is the final string: --allow-root | awk '{ if( index($2,"mydomain.com") != 0 ) print $1; }' OR --allow-root | awk '{ if( index($2,"blog.example.com/site9") != 0 ) print $1; }' Depending on how you are structuring your URL's in wordpress – Jay Lepore Jan 5 '18 at 20:31
  • Glad to hear it helped. Thanks for sharing your workaround. There could also be other workarounds, like using wp db query .... but I think it's not recommended in general to run wp-cli as root with --allow-root. Another thing that comes to mind is to watch out for cases like mydomain.com and thisismydomain.com when using index/substring matches. – birgire Jan 5 '18 at 20:53
  • I'm using this in a bash file script ie; --------------- returns nothing with $wpurl ie; -------------------- wp site list --path="$pathtowordpress" --allow-root --color | awk '{ if( index($2,"$wpurl") != 0 ) print $1; }' ---------------------------- Have tried inserting variable with single quotes, double quotes and ".$wpurl." but all return empty so I'm sure it's not processing that variable correctly.----- When I try remove quotes entirely, it just errors out on the period of the .com section. .... Any advice ? – Jay Lepore Jan 5 '18 at 20:57
  • I updated the answer with an example. Hope it works for you. – birgire Jan 5 '18 at 21:20
  • Worked like a charm! I noticed you went in a different direction than index. Thank you. Resolved again ;-) – Jay Lepore Jan 5 '18 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.