Skip to content

Instantly share code, notes, and snippets.

@mihdan
Forked from dangtrinhnt/get_blogid_from_url.sh
Created February 15, 2024 22:26
Show Gist options
  • Save mihdan/1667da8716f47d15a3e2da28d1304f01 to your computer and use it in GitHub Desktop.
Save mihdan/1667da8716f47d15a3e2da28d1304f01 to your computer and use it in GitHub Desktop.
Get blog_id from url using wp-cli
#! /bin/bash
# example csv file, mycsv.csv:
#
# path,some_field
# somepath,some_value
# anotherpath,another_value
# ...
#
# run the following command:
# ./get_blogid_from_url.sh mycsv.csv myblog.com /wordpress/path > result.csv
#
# get network's blog list in csv format
sites=$(wp site list --allow-root --path=$3 --fields=blog_id,url --format=csv)
# parse the input csv file
IFS=','
while read a1 a2
do
# parse the network's blog list
echo "$sites" | while read f1 f2
do
# remove the carriage return from the last column
blog_url=$(echo $f2 | sed -e 's/\r//g')
if [ $blog_url == "$2/$a1/" ]
then
echo "$f1,$blog_url"
fi
done
done < $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment