Skip to content

Instantly share code, notes, and snippets.

@khoipro
Created January 4, 2024 14:48
Show Gist options
  • Save khoipro/889cbdfbe6d9ca8c102935fdf8c03892 to your computer and use it in GitHub Desktop.
Save khoipro/889cbdfbe6d9ca8c102935fdf8c03892 to your computer and use it in GitHub Desktop.
Sample way to delete post tags by ids (export from Google Sheets with rows)
<?php
// Step 1: Copy all Google Sheets' row and process to only number with commas
// It looks like
// 197
// 112
// 256
// https://www.browserling.com/tools/text-rows-to-columns
// Step 2: Open with any IDE such like Visual Studio Code to remove all whitespace, eg: "192 ,91" => "192,91"
// Step 3: Place a term ids list replace $$raw_term_ids to remove it.
add_action('wp', 'process_delete_post_tags', 100);
function process_delete_post_tags() {
if ( is_user_logged_in() && !empty( $_GET['process_delete_terms' ] ) ) :
$raw_term_ids = "197,112,256";
$term_ids = explode(',', $raw_term_ids);
wp_defer_term_counting(true);
$success = 0;
$failure = 0;
foreach ($term_ids as $term_id) {
$result = wp_delete_term( $term_id, 'post_tag' );
if ($result) {
$success++;
} else {
$failure++;
}
}
wp_defer_term_counting(false);
echo '<pre>';
echo 'Success: ' . $success . PHP_EOL;
echo 'Failure: ' . $failure . PHP_EOL;
echo '</pre>';
exit;
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment