Skip to content

Instantly share code, notes, and snippets.

@vpadhariya
Last active August 10, 2017 08:21
Show Gist options
  • Save vpadhariya/fd4e85f68b71da9c9d8915d993ece225 to your computer and use it in GitHub Desktop.
Save vpadhariya/fd4e85f68b71da9c9d8915d993ece225 to your computer and use it in GitHub Desktop.
In wordpress there were some certain requirement if the category was deleted then we need to Trash the posts attached to theme, here is the code how to achieve this easily.
<?php
/**
* If any category is deleted move all attached posts into trash
*/
function trash_category_posts($object_id, $tt_ids)
{
/**
* 1. Check the post_type
* 2. Check the action is delete or delete-tag
* 3. Check the taxonomy type is "category"
*/
if(get_post_type($object_id) == "post" && ($_POST['action'] == 'delete' || $_POST['action'] == 'delete-tag') && $_POST['taxonomy'] == "category")
{
// Trash the post so it will be deleted after EMPTY_TRASH_DAYS
wp_trash_post($object_id);
}
}
add_action("delete_term_relationships", "trash_category_posts");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment