From 1ea46c905593016d4a2d0bdd399fbea1e61e2289 Mon Sep 17 00:00:00 2001 From: Jason Raveling Date: Fri, 27 Feb 2026 08:56:38 -0600 Subject: [PATCH 1/3] feat(media): adds script for searching media library by post title --- search-posts-media-library.sh | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 search-posts-media-library.sh diff --git a/search-posts-media-library.sh b/search-posts-media-library.sh new file mode 100644 index 0000000..b965be5 --- /dev/null +++ b/search-posts-media-library.sh @@ -0,0 +1,61 @@ +#!/bin/env bash + +# Search the media library post title on every network site. Return as +# a CSV. + +source 'source/includes.sh'; + +# The text to search for in the title. +search_regex=''; # Leave empty to return all. + +# The file to save results to. +output_file='search-results.txt'; + +# Reset the output file contents. +echo '' > $output_file; + +echo; +echo 'Beginning search...'; + +# Get every network site. +all_site_ids=$(wp_skip_all site list --field="blog_id"); + +# Loop over every network site. +for site_id in $all_site_ids; do + + if [[ 1 -eq $site_id ]]; then + continue; + fi + + echo >> $output_file; + echo "Results for Site ID ${site_id}" >> $output_file; + + # Search for multi term phrases. Cases sensitive. + wp_skip_all db query --skip-column-names "SELECT ${fields} FROM wp_${site_id}_posts WHERE post_type = 'attachment' AND post_REGEXP '${search_regex}';" >> $output_file; + + # The SQL query for searching the media library of a network site. + sql_query="SELECT + $site_id as site_id, + p.ID, + p.post_title, + p.post_mime_type, + pm1.meta_value AS file_path, + p.guid AS URL + FROM + wp_${site_id}_posts p + LEFT JOIN + wp_3_postmeta pm1 ON (p.ID = pm1.post_id AND pm1.meta_key = '_wp_attached_file') + WHERE + p.post_type = 'attachment'" + + # Check if a search string was provided. + if [ -n "$search_regex" ]; then + QUERY="$QUERY AND p.post_title REGEXP '${search_regex}'" + fi + + wp_skip_all db query --skip-column-names "SELECT ${fields} FROM wp_${site_id}_posts WHERE post_type = 'attachment' AND post_REGEXP '${search_regex}';" tee -a $output_file; + +done; + +echo "Finished search. Results are in ${output_file}"; +echo; From 7c87da7b966ceb20ddbf39634dff96ff7284e345 Mon Sep 17 00:00:00 2001 From: Jason Raveling Date: Fri, 27 Feb 2026 09:26:52 -0600 Subject: [PATCH 2/3] feat(media): adds script for searching media library by post title --- search-posts-media-library.sh | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/search-posts-media-library.sh b/search-posts-media-library.sh index b965be5..4c9f6d4 100644 --- a/search-posts-media-library.sh +++ b/search-posts-media-library.sh @@ -27,12 +27,6 @@ for site_id in $all_site_ids; do continue; fi - echo >> $output_file; - echo "Results for Site ID ${site_id}" >> $output_file; - - # Search for multi term phrases. Cases sensitive. - wp_skip_all db query --skip-column-names "SELECT ${fields} FROM wp_${site_id}_posts WHERE post_type = 'attachment' AND post_REGEXP '${search_regex}';" >> $output_file; - # The SQL query for searching the media library of a network site. sql_query="SELECT $site_id as site_id, @@ -50,10 +44,10 @@ for site_id in $all_site_ids; do # Check if a search string was provided. if [ -n "$search_regex" ]; then - QUERY="$QUERY AND p.post_title REGEXP '${search_regex}'" + sql_query="$sql_query AND p.post_title REGEXP '${search_regex}'" fi - wp_skip_all db query --skip-column-names "SELECT ${fields} FROM wp_${site_id}_posts WHERE post_type = 'attachment' AND post_REGEXP '${search_regex}';" tee -a $output_file; + wp_skip_all db query --skip-column-names "$sql_query" | tee -a $output_file; done; From 207817ba998b9f4ac4be9d41162ce0ce16fe1cc6 Mon Sep 17 00:00:00 2001 From: Jason Raveling Date: Fri, 27 Feb 2026 12:07:20 -0600 Subject: [PATCH 3/3] feat(media): add search of media library, output as csv --- search-posts-media-library.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/search-posts-media-library.sh b/search-posts-media-library.sh index 4c9f6d4..45611c1 100644 --- a/search-posts-media-library.sh +++ b/search-posts-media-library.sh @@ -38,7 +38,7 @@ for site_id in $all_site_ids; do FROM wp_${site_id}_posts p LEFT JOIN - wp_3_postmeta pm1 ON (p.ID = pm1.post_id AND pm1.meta_key = '_wp_attached_file') + wp_${site_id}_postmeta pm1 ON (p.ID = pm1.post_id AND pm1.meta_key = '_wp_attached_file') WHERE p.post_type = 'attachment'" @@ -47,7 +47,8 @@ for site_id in $all_site_ids; do sql_query="$sql_query AND p.post_title REGEXP '${search_regex}'" fi - wp_skip_all db query --skip-column-names "$sql_query" | tee -a $output_file; + # Run the wpcli command | use sed to replace tabs with commas | output to shell and file. + wp_skip_all db query --skip-column-names "$sql_query" | sed -E 's/"/""/g; s/\t/","/g; s/^/"/; s/$/"/' | tee -a $output_file; done;