File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #! /bin/env bash
2+
3+ # Search the media library post title on every network site. Return as
4+ # a CSV.
5+
6+ source ' source/includes.sh' ;
7+
8+ # The text to search for in the title.
9+ search_regex=' ' ; # Leave empty to return all.
10+
11+ # The file to save results to.
12+ output_file=' search-results.txt' ;
13+
14+ # Reset the output file contents.
15+ echo ' ' > $output_file ;
16+
17+ echo ;
18+ echo ' Beginning search...' ;
19+
20+ # Get every network site.
21+ all_site_ids=$( wp_skip_all site list --field=" blog_id" ) ;
22+
23+ # Loop over every network site.
24+ for site_id in $all_site_ids ; do
25+
26+ if [[ 1 -eq $site_id ]]; then
27+ continue ;
28+ fi
29+
30+ # The SQL query for searching the media library of a network site.
31+ sql_query=" SELECT
32+ $site_id as site_id,
33+ p.ID,
34+ p.post_title,
35+ p.post_mime_type,
36+ pm1.meta_value AS file_path,
37+ p.guid AS URL
38+ FROM
39+ wp_${site_id} _posts p
40+ LEFT JOIN
41+ wp_${site_id} _postmeta pm1 ON (p.ID = pm1.post_id AND pm1.meta_key = '_wp_attached_file')
42+ WHERE
43+ p.post_type = 'attachment'"
44+
45+ # Check if a search string was provided.
46+ if [ -n " $search_regex " ]; then
47+ sql_query=" $sql_query AND p.post_title REGEXP '${search_regex} '"
48+ fi
49+
50+ # Run the wpcli command | use sed to replace tabs with commas | output to shell and file.
51+ wp_skip_all db query --skip-column-names " $sql_query " | sed -E ' s/"/""/g; s/\t/","/g; s/^/"/; s/$/"/' | tee -a $output_file ;
52+
53+ done ;
54+
55+ echo " Finished search. Results are in ${output_file} " ;
56+ echo ;
You can’t perform that action at this time.
0 commit comments