10

I need to create an "Empty Trash" button inside my plugin. How would I do it using PHP code?

9

You can use wp_delete_post.

To get all posts with the "trash" status:

$trash = get_posts('post_status=trash&numberposts=-1');

Then:

foreach($trash as $post)
  wp_delete_post($post->ID, $bypass_trash = true);
  • Cheers! It works! – Ciprian Oct 1 '12 at 11:35
0

This didn't work for me. I had to do the following:

$args = array(
'posts_per_page'   => -1,
'post_status'      => 'trash'
    );

$trash = get_posts($args);

foreach($trash as $post)
{
    wp_delete_post($post->ID, true);      
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.