WordPress: Difference between “trash” and “delete” functions 

Filed in WordPress Tags: , , , ,

WordPress has two native functions for removing posts. The traditional function, wp_delete_post, originated back in the day and has been used for a long while by post-related plugins. A second function, wp_trash_post, was added in version 2.9. Here’s a short explanation.

wp_delete_post( POSTID, TRUE );
// TRUE|FALSE: Bypass trash. Defaults to FALSE.
wp_trash_post( POSTID );

In version 3, WordPress peeps let out a sigh of relief with the advent of custom post types. No longer would we need to use custom fields to denote a special type of post! Unfortunately, the old school method of removal, wp_delete_post, isn’t equipped to handle custom post types.

wp_delete_post('399', FALSE);

This example will immediately trash whatever 399 is linked to…assuming it’s a standard post or page. wp_delete_post makes a determination when it runs: if the ID doesn’t belong to a post or a page, then it’s removed. If it’s a custom post, tough cookies, it’s insta-deleted, totally bypassing the trash.

The workaround is to use the newer function, wp_trash_post. This just accepts a post ID, and doesn’t particularly care about what type of entry it is. It simply changes the status to trash. If you give it something not trash worthy, and your data disappears into the database ether, that’s your problem.

Upshot: Unless you want to immediately, permanently delete the data, use wp_trash_post.

Feedback

Comments

One Response to “WordPress: Difference between “trash” and “delete” functions”
  1. Brad Trivers says:

    Thank you, thank you, thank you! The new function wp_trash_post is not referenced in the codex and I was wondering why I couldn’t get my custom posts to go to the trash with wp_delete_post.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About Me


I'm Becky. I write this blog. From time to time, someone else will write this blog. I like WordPress, hiking and puppies.

You can hire me for WordPress or PHP odds n' ends on oDesk.