Skip to content

Instantly share code, notes, and snippets.

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 rinatkhaziev/22b6b389d5e42d9b48dee64cfbf3766f to your computer and use it in GitHub Desktop.
Save rinatkhaziev/22b6b389d5e42d9b48dee64cfbf3766f to your computer and use it in GitHub Desktop.
WP CLI: list terms with the same name in a hierarchical taxonomy for sites in multisite network.
<?php
/**
* List terms (categories) with the same name for each of the blog with a sticker.
*
* This is a one-off, but could be easily abstracted to be more versatile.
*
* @subcommand list-multiple-terms-with-the-same-name
* @synopsis [--taxonomy=<category>]
*/
function list_multiple_terms_with_the_same_name( $args, $assoc_args ) {
$tax = $assoc_args['taxonomy'] ?? 'category';
$current_blog_id = get_current_blog_id();
// Can be list of blog ids in the network
$blog_ids = [ $current_blog_id ];
$ret = [];
$get_function = function() {
$categories = get_terms( $tax, [ 'hide_empty' => false, 'fields' => 'id=>name' ] );
return array_unique( array_diff_assoc( $categories, array_unique( $categories ) ) );
};
WP_CLI::line( sprintf( 'Found %d blogs with "%s" sticker', count( $blog_ids ), $sticker ) );
foreach( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
$dupes = $get_function();
if ( $dupes ) {
$ret[ $blog_id ] = [
'bloginfo' => [
'name' => get_bloginfo( 'name' ),
'url' => get_bloginfo( 'url' ),
],
'duplicateCategories' => array_map( function( $cat ) { return sprintf("%s : %s", $cat, esc_url_raw( admin_url( "edit-tags.php?taxonomy=category&post_type=post&s={$cat}" ) ) ); }, array_values( $dupes ) )
];
}
$this->stop_the_insanity();
}
WP_CLI::line( sprintf( '%d blogs have categories with duplicate names', count( $ret ) ) );
WP_CLI::line( wp_json_encode( $ret, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) );
switch_to_blog( $current_blog_id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment