Skip to content

Instantly share code, notes, and snippets.

@twentyfortysix
Created May 2, 2017 15:19
Show Gist options
  • Save twentyfortysix/e785af09e65bbbdc2905905dcb1edc17 to your computer and use it in GitHub Desktop.
Save twentyfortysix/e785af09e65bbbdc2905905dcb1edc17 to your computer and use it in GitHub Desktop.
WP - bulk remove category
<?php
add_action( 'init', function()
{
// Get all the posts which is assigned to the uncategorized category
$args = array(
'post_type' => 'cp_crq_news',
'posts_per_page' => -1, // Adjust as needed
'cat' => 84, // Category ID for uncategorized category
'fields' => 'ids', // Only get post ID's for performance
// Add any additional args here, see WP_Query
);
$q = get_posts( $args );
// Make sure we have posts
if ( !$q )
return;
// We have posts, lets loop through them and remove the category
foreach ( $q as $id )
wp_remove_object_terms(
$id, // Post ID
84, // Term ID to remove
'category' // The taxonomy the term belongs to
);
}, PHP_INT_MAX );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment