Skip to content

Instantly share code, notes, and snippets.

@boonebgorges
Last active August 29, 2015 14:21
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 boonebgorges/2beb1379569c3bd85803 to your computer and use it in GitHub Desktop.
Save boonebgorges/2beb1379569c3bd85803 to your computer and use it in GitHub Desktop.
<?php
// Bail if WP-CLI is not present.
if ( !defined( 'WP_CLI' ) ) return;
class BBG_Command extends WP_CLI_Command {
public function generate_shared_terms( $args, $assoc_args ) {
global $wpdb;
$defaults = array(
'count' => 10,
'from_taxonomy' => 'post_tag',
'to_taxonomy' => 'category',
);
$r = array_merge( $defaults, $assoc_args );
// Create some base terms in the 'category' taxonomy.
$_args = array( 'term', 'generate', 'category' );
$_assoc_args = array( 'count' => $r['count'], 'max_depth' => 4 );
WP_CLI::run_command( $_args, $_assoc_args );
// Get the IDs of the terms just created.
$source_term_ids = $wpdb->get_col( "SELECT * FROM $wpdb->terms ORDER BY term_id DESC limit " . intval( $r['count'] ) );
$c = array(
'foo1' => array( 'start' => $source_term_ids[0], 'count' => floor( $r['count'] / 2 ) ),
'foo2' => array( 'start' => $source_term_ids[0], 'count' => $r['count'] - 1 ),
'foo3' => array( 'start' => $source_term_ids[0], 'count' => floor( $r['count'] / 3 ) ),
'foo4' => array( 'start' => $source_term_ids[0], 'count' => floor( $r['count'] / 2 ) ),
);
foreach ( $c as $ctax => $cdata ) {
register_taxonomy( $ctax, 'post' );
$to_tax_obj = get_taxonomy( $ctax );
$label = $to_tax_obj->labels->singular_name;
$notify = \WP_CLI\Utils\make_progress_bar( 'Generating shared terms in taxonomy ' . $ctax, $r['count'] );
for ( $i = $cdata['start']; $i < ( $cdata['start'] + $cdata['count'] ); $i++ ) {
// Create a new term.
$term = wp_insert_term( $label . ' ' . rand( 0, 9999999 ), $ctax );
if ( is_wp_error( $term ) ) {
WP_CLI::warning( $term );
} else {
$wpdb->update( $wpdb->term_taxonomy,
array( 'term_id' => $i ),
array( 'term_taxonomy_id' => $term['term_taxonomy_id'] )
);
}
$notify->tick();
}
$notify->finish();
}
}
}
WP_CLI::add_command( 'bbg', 'BBG_Command' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment