2

I need to search through dozens of WordPress multisite (network enabled) installs for sites that use a particular plugin.

How can I list only sites that use a particular theme or plugin, preferably with either WP CLI or a database query?

0

WP CLI is the easiest option here.

First, we want an array/list we can store the desired sites in:

results=()

Then, we get a list of all sites as a variable, and loop over them:

blogs=$(wp site list --fields="blog_id" --format="csv")
for entry in $blogs
do
    if [ "${entry}" == "blog_id" ]; then
        continue # skip the CSV header
    fi

Next, grab the sites active theme:

    theme=$(wp theme list --status=active --format=csv --fields="name" --url="${entry}" | sed -n 2p)

Here I'm using | sed -n 2p to get the second line of output in order to skip the CSV header.

Then, if the sites theme matches what we're looking for, add it to a list:

    if $theme == 'mytheme';
    if [ "${theme}" == "mytheme" ]; then
        results+=($entry)
    fi

Then we close our loop

done

Turn our list into a comma separated string, and pass it to wp site list via --sites__in:

wp site list --site__in="${results[*]}"

We could also swap the retrieval of the theme and the check for a wp plugin is-active call if we wanted to filter by active plugin, e.g. wp plugin is-active hello

https://developer.wordpress.org/cli/commands/plugin/is-active/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.