Created
November 18, 2021 15:51
-
-
Save petenelson/8063bfe1999403e98c67c7ba48c22ddc to your computer and use it in GitHub Desktop.
WordPress: Term Report CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
/** | |
* Generate a term list/report. | |
* | |
* ## OPTIONS | |
* | |
* <taxonomy> | |
* : The taxonomy name. | |
* | |
* [--post_type=<post_type>] | |
* : Optional comma-delimited list of post types, defaults to all post types. | |
* | |
* [--format=<format>] | |
* : Render output in a particular format. Table only shows a max of 100 terms. | |
* --- | |
* default: table | |
* options: | |
* - table | |
* - csv | |
* - json | |
* - yaml | |
* --- | |
* | |
* @param array $args List of args. | |
* @param array $assoc_args List of assoc args. | |
* @return void | |
*/ | |
function term_report( $args, $assoc_args = [] ) { | |
$taxonomy = $args[0]; | |
$post_types = isset( $assoc_args['post_type'] ) && ! empty( $assoc_args['post_type'] ) ? $assoc_args['post_type'] : ''; | |
$post_types = explode( ',', $post_types ); | |
$post_types = array_filter( $post_types ); | |
$post_types = empty( $post_types ) ? 'any' : $post_types; | |
$format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'table'; | |
if ( ! taxonomy_exists( $taxonomy ) ) { | |
\WP_CLI::error( sprintf( 'Taxonomy %s does not exist', $taxonomy ) ); | |
} | |
foreach ( $post_types as $post_type ) { | |
if ( ! post_type_exists( $post_type ) ) { | |
\WP_CLI::error( sprintf( 'Post type %s does not exist', $post_type ) ); | |
} | |
} | |
if ( 'table' === $format ) { | |
\WP_CLI::line( sprintf( 'Getting term list for taxonomy %s...', $taxonomy ) ); | |
} | |
$term_query_args = [ | |
'hide_empty' => false, | |
'update_term_meta_cache' => false, | |
'orderby' => 'count', | |
'order' => 'DESC', | |
]; | |
if ( 'table' === $format ) { | |
$term_query_args['number'] = 100; | |
} | |
$terms = get_terms( $taxonomy, $term_query_args ); | |
$progress_bar = false; | |
if ( 'table' === $format ) { | |
$progress_bar = \WP_CLI\Utils\make_progress_bar( 'Generating Report', count( $terms ) ); | |
} | |
$header = [ 'Name', 'Slug', 'Count', 'Last Used' ]; | |
$items = []; | |
foreach ( $terms as $term ) { | |
$item = [ | |
'Name' => $term->name, | |
'Slug' => $term->slug, | |
'Count' => $term->count, | |
'Last Used' => '', | |
]; | |
$query_args = [ | |
'post_type' => $post_types, | |
'posts_per_page' => 1, | |
'update_post_meta_cache' => false, | |
'update_term_meta_cache' => false, | |
'tax_query' => [ | |
[ | |
'taxonomy' => $taxonomy, | |
'field' => 'term_id', | |
'terms' => $term->term_id, | |
] | |
], | |
]; | |
$query = new \WP_Query( $query_args ); | |
$item['Count'] = $query->found_posts; | |
if ( ! empty( $query->posts ) ) { | |
$item['Last Used'] = $query->posts[0]->post_modified; | |
} | |
$items[] = $item; | |
if ( ! empty( $progress_bar ) ) { | |
$progress_bar->tick(); | |
} | |
} | |
if ( ! empty( $progress_bar ) ) { | |
$progress_bar->finish(); | |
} | |
\WP_CLI\Utils\format_items( $format, $items, $header ); | |
} | |
\WP_CLI::add_command( 'ipm term-report', __NAMESPACE__ . '\term_report' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment