Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to delete a post with its all attachments. This is the function that i came up with right now;

function remove_post(){
            if(isset($_POST['post_id']) && is_numeric($_POST['post_id'])){
                $post = get_post($_POST['post_id']);
                if(get_current_user_id() == $post->post_author){ echo 'ee';
                    wp_delete_post($_POST['post_id']);
                }
            }  

            exit;
        }

This deletes the post but not the attachments link to that post also its not hard deleting, the post remains in the trash. So what do you think? Thanks.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Maybe this works

function remove_post() {

            if(isset($_POST['post_id']) && is_numeric($_POST['post_id'])) {

                $post = get_post($_POST['post_id']);

                if(get_current_user_id() == $post->post_author) { 


                     $args = array(
                             'post_parent' => $_POST['post_id']
                 );

                 $post_attachments = get_children($args);

                     if($post_attachments) {

                            foreach ($post_attachments as $attachment) {

                                      wp_delete_attachment($attachment->ID, true);

                            }

                     }

                          wp_delete_post($_POST['post_id'], true);

                }

            }  

            exit;
}

The code added

function get_attachment_id_from_src ($image_src) {
  global $wpdb;

  $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
  $id = $wpdb->get_var($query);

  return $id;

}

if(!empty($_POST['avatar_id'])  && $_POST['avatar_id'] != get_user_meta($current_user->id, 'custom_avatar', true) && empty( $_POST['remove_avatar'])) {

        update_user_meta($current_user->id, 'custom_avatar', esc_attr( $_POST['avatar_id']));

    } elseif(!empty( $_POST['remove_avatar']) && $_POST['remove_avatar'] == 1) {

                $avatar_url = get_user_meta($current_user->id, 'custom_avatar', true);

                $attachment_delete = get_attachment_id_from_src($avatar_url);

                wp_delete_attachment($attachment_delete, true);

        update_user_meta( $current_user->id, 'custom_avatar', '-1');

    }
share|improve this answer
    
didnt work for me :( –  andresmijares25 Nov 23 '11 at 23:07
    
it worked but attachments remained –  boranb Nov 23 '11 at 23:10
    
Try again I did a few changes to the function, meavy that you saw was the cache, you can avoid this with Ctrl + F5, the images should be delete in a short time –  Andres Yanez Nov 24 '11 at 3:34
    
it worked! thank you very much Andres! –  boranb Nov 24 '11 at 8:44
    
Andres i also using custom_avatar meta to store the users uploaded avatars. I am removing the avatar like that; pastebin.com/7Dj9Cp9j it removes the avatar but the uploaded files remains. how can i add your solution to this to permanently delete the uploaded images from the server? thanks. –  boranb Nov 24 '11 at 13:14

See the codex for the correct use of wp_delete_post, use

wp_delete_post( $_POST['id'], true );

to bypass the trash

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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