Skip to content

Instantly share code, notes, and snippets.

@PatelUtkarsh
Last active April 24, 2024 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatelUtkarsh/45cdc740eaa0cb544affb76f1422270d to your computer and use it in GitHub Desktop.
Save PatelUtkarsh/45cdc740eaa0cb544affb76f1422270d to your computer and use it in GitHub Desktop.
WP-CLI script to remove extra tags from WordPress on multisite
#!/bin/bash
# Get all site urls
site_urls=$(wp site list --field=url)
# Loop through each site url
for site_url in $site_urls
do
# Recount all post tags
wp term recount post_tag --url=$site_url --skip-plugins --skip-themes
# Get all post tags
tags=$(wp term list post_tag --fields=term_id,count --format=csv --url=$site_url --skip-plugins --skip-themes)
# Initialize empty string for ids to delete
ids_to_delete=""
# Loop through each line of the output
echo "$tags" | tail -n +2 | while IFS=, read -r id count; do
# If the count is 0, add the id to the list of ids to delete
if [ "$count" -eq 0 ]; then
ids_to_delete="$ids_to_delete $id"
fi
done
# If there are ids to delete, delete them all in one go
if [ ! -z "$ids_to_delete" ]; then
echo "Deleting terms: $ids_to_delete"
# wp term delete post_tag $ids_to_delete --url=$site_url --skip-plugins --skip-themes
fi
sleep 10
done
@PatelUtkarsh
Copy link
Author

PatelUtkarsh commented Apr 24, 2024

WPEngine output from recount:

KILLED QUERY (255570 characters long generated in phar:///usr/local/bin/wp/vendor/wp-cli/entity-command/src/Term_Command.php:640): SELE
CT t.*, tt.* FROM wp_13_terms AS t INNER JOIN wp_13_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (15460,42519,32007
,43100,42965,42107,42250,42302,42245,4265....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment