Last active
December 25, 2015 04:39
-
-
Save tlovett1/6918782 to your computer and use it in GitHub Desktop.
A WP-CLI command to tag all posts without a term with a different term
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 | |
/** | |
* Migrate posts to scoops | |
* | |
* @subcommand migrate-scoops | |
*/ | |
public function migrate_scoops( $args, $assoc_args ) { | |
global $wpdb; | |
$scoops_term = get_term_by( 'slug', 'sccops-analysis', 'pehub_section' ); | |
if ( empty( $scoops_term ) ) | |
WP_CLI::error( 'Sccops term not found.' ); | |
$count = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts as posts WHERE posts.post_type='post' AND posts.ID NOT IN ( SELECT posts.ID FROM $wpdb->posts as posts, $wpdb->term_relationships as term_relationships, $wpdb->term_taxonomy as term_taxonomy, $wpdb->terms as terms WHERE post_type='post' AND posts.ID = term_relationships.object_id AND term_relationships.term_taxonomy_id = term_taxonomy.term_taxonomy_id AND term_taxonomy.term_id = terms.term_id AND terms.slug = 'news-digest' AND term_taxonomy.taxonomy = 'pehub_section' )" ); | |
$chunk_size = 500; | |
$num_chunks = ceil( $count / $chunk_size ); | |
$total_tagged = 0; | |
for ( $i = 0; $i < $num_chunks; $i++ ) { | |
$posts = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts as posts WHERE posts.post_type='post' AND posts.ID NOT IN ( SELECT posts.ID FROM $wpdb->posts as posts, $wpdb->term_relationships as term_relationships, $wpdb->term_taxonomy as term_taxonomy, $wpdb->terms as terms WHERE post_type='post' AND posts.ID = term_relationships.object_id AND term_relationships.term_taxonomy_id = term_taxonomy.term_taxonomy_id AND term_taxonomy.term_id = terms.term_id AND terms.slug = 'news-digest' AND term_taxonomy.taxonomy = 'pehub_section' ) LIMIT %d, %d", ( $i * $chunk_size ), $chunk_size ) ); | |
foreach ( $posts as $post ) { | |
$wpdb->insert( | |
$wpdb->, | |
array( | |
'object_id' => (int) $post->ID, | |
'term_taxonomy_id' => (int) $scoops_term->term_taxonomy_id, | |
'term_order' => 0, | |
), | |
array( | |
'%d', | |
'%d' , | |
'%d' | |
) | |
); | |
$total_tagged++; | |
} | |
} | |
WP_CLI::success( $total_tagged . ' posts tagged.' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment