Skip to content

Instantly share code, notes, and snippets.

@celian-m
Forked from michael-sumner/index.sh
Created July 24, 2024 17:22
Show Gist options
  • Save celian-m/dbff452ab80f081d4ccf0c032aec66ee to your computer and use it in GitHub Desktop.
Save celian-m/dbff452ab80f081d4ccf0c032aec66ee to your computer and use it in GitHub Desktop.
WP-CLI command to loop through all sites in the multisite network and use wp db query to obtain a list of posts, and save them in log.txt file
# replace "Hello, World!" with appropriate string to search for
# replace `post_title` with appropriate column to search for
wp site list --fields=url,blog_id | while IFS= read -r line; do
url=$(echo "$line" | awk -F'\t' '{print $1}')
blog_id=$(echo "$line" | awk -F'\t' '{print $2}')
wp post list --field=url --ignore_sticky_posts=1 --orderby=date --order=DESC --post__in=$(wp db query "SELECT ID FROM wp_${blog_id}_posts WHERE post_title LIKE '%Hello, World!%' AND post_status='publish' AND post_type='post'" --skip-column-names --url=${url} | paste -s -d ',' -) --url=${url}
done >> log.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment