Skip to content

Instantly share code, notes, and snippets.

@PatelUtkarsh
Last active April 24, 2024 09:30
Show Gist options
  • Select an option

  • Save PatelUtkarsh/45cdc740eaa0cb544affb76f1422270d to your computer and use it in GitHub Desktop.

Select an option

Save PatelUtkarsh/45cdc740eaa0cb544affb76f1422270d to your computer and use it in GitHub Desktop.

Revisions

  1. PatelUtkarsh revised this gist Apr 24, 2024. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion clean-tags.sh
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,8 @@ do

    # If there are ids to delete, delete them all in one go
    if [ ! -z "$ids_to_delete" ]; then
    wp term delete post_tag $ids_to_delete --url=$site_url --skip-plugins --skip-themes
    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
  2. PatelUtkarsh revised this gist Apr 24, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions clean-tags.sh
    Original file line number Diff line number Diff line change
    @@ -27,4 +27,5 @@ do
    if [ ! -z "$ids_to_delete" ]; then
    wp term delete post_tag $ids_to_delete --url=$site_url --skip-plugins --skip-themes
    fi
    sleep 10
    done
  3. PatelUtkarsh created this gist Apr 24, 2024.
    30 changes: 30 additions & 0 deletions clean-tags.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/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
    wp term delete post_tag $ids_to_delete --url=$site_url --skip-plugins --skip-themes
    fi
    done