Skip to content

Instantly share code, notes, and snippets.

@Niloys7
Created April 17, 2021 12:44
Show Gist options
  • Save Niloys7/e6e23323a1ff3e9d95d3357d925a431b to your computer and use it in GitHub Desktop.
Save Niloys7/e6e23323a1ff3e9d95d3357d925a431b to your computer and use it in GitHub Desktop.
//Delete all posts of a custom post type with its included post meta data and taxonomies by using wpdb
function deletePostType($postType = ''){
global $wpdb;
$result = $wpdb->query(
$wpdb->prepare("
DELETE posts,pt,pm
FROM {$wpdb->prefix}posts posts
LEFT JOIN {$wpdb->prefix}term_relationships pt ON pt.object_id = posts.ID
LEFT JOIN {$wpdb->prefix}postmeta pm ON pm.post_id = posts.ID
WHERE posts.post_type = %s
",
$post_type
)
);
return $result!==false;
}
deletePostType('shop_deposit'); // add custom post type slug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment