Forked from jazibsawar/delete_custom_post_type.php
Created
November 14, 2017 14:38
-
-
Save netEmmanuel/3d11f199bcea1f7cace6c76997b07b7e to your computer and use it in GitHub Desktop.
WordPress $wpdb: Delete all posts of a custom post type with its associated meta data (taxonomies, post meta) using SQL query & $wpdb
This file contains 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 | |
function delete_custom_posts($post_type = 'post'){ | |
global $wpdb; | |
$result = $wpdb->query( | |
$wpdb->prepare(" | |
DELETE posts,pt,pm | |
FROM wp_posts posts | |
LEFT JOIN wp_term_relationships pt ON pt.object_id = posts.ID | |
LEFT JOIN wp_postmeta pm ON pm.post_id = posts.ID | |
WHERE posts.post_type = %s | |
", | |
$post_type | |
) | |
); | |
return $result!==false; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment