I would like to know if just using wp_delete_post($post_id, true);
is enough to delete everything in DB that is tied to the post, skipping the trash.
According to the documentation it is, but there are several answers on the internet mentioning to use delete_post_meta also, like this:
// Delete all Post Meta
foreach (get_post_meta($post_id) as $key => $val) {
delete_post_meta($post_id, $key);
}
wp_delete_post($post_id, true);
What’s the best practice?
2
Answers
YES. Using wp_delete_post($post_id, true) deletes all post-related information from the database, including attachments and post meta data, and bypasses the trash, resulting in immediate deletion. But, if you want to delete post meta data associated with the post, you should use delete_post_meta() function in addition to wp_delete_post() function.
According to the source on the page you linked,
wp_delete_post($post_id, true);
deletes all the metadata and comments linked to the post. It force deletes the post and does therefore bypass the trash. As an answer to your question, it is enough.Reference in code (wp-includes/post.php:3438-3448):
If you would only want to delete post meta, please refrain of using
wp_delete_post()
and do instead usedelete_post_meta()
.