Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions search-posts-media-library.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/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

# 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_${site_id}_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
sql_query="$sql_query AND p.post_title REGEXP '${search_regex}'"
fi

# 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;

echo "Finished search. Results are in ${output_file}";
echo;