How do I remove “Uncategorized” from posts with more than 1 category?

With wp-cli installed you can run a bash script like this to remove the ‘uncategorized’ category from all posts with more than one category

#!/bin/bash

for post in $(wp post list --field=ID)
do
  count=$(wp post term list $post 'category' --fields="name" --format="count")
  if [ "$count" -gt "1" ]
  then
    wp post term remove $post category 'uncategorized'
  fi
done

Save this as something like delete_uncategorized.bash and then run bash delete_uncategorized.bash from the command line.

Leave a Comment