admin管理员组

文章数量:1277296

I found this code.

But not work for me. Where to insert the code? I try

wp-content/theme/mytheme/functions.php - not work.

wp-includes/post.php - not work.

wp-includes/functions.php - not work.

And I make plugin (but this the first), not work. Plugin download.

My Wordpress version is 4.2.2.

Big Thanks, and sorry I'm rookie!

I found this code.

But not work for me. Where to insert the code? I try

wp-content/theme/mytheme/functions.php - not work.

wp-includes/post.php - not work.

wp-includes/functions.php - not work.

And I make plugin (but this the first), not work. Plugin download.

My Wordpress version is 4.2.2.

Big Thanks, and sorry I'm rookie!

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked May 15, 2015 at 7:50 AronAron 133 bronze badges 3
  • 1 which solution you are trying to use (out of the two, given in so question)? I'd suggest not to edit the WordPress core files directly, wp-includes/post.php, wp-includes/functions.php, never edit those files – Kumar Commented May 15, 2015 at 9:23
  • I trying insert the code in above php - not work. After I trying make plugin (link above) - not work. Okey, I don't edit core files! (My English maybe not good sorry) – Aron Commented May 15, 2015 at 10:23
  • 1 possible duplicate of Delete Associated Media Upon Page Deletion – TheDeadMedic Commented May 15, 2015 at 10:49
Add a comment  | 

1 Answer 1

Reset to default 3

Just for clarity: borrowing from this answer, add the following to your theme's functions.php:

function wpse_188427_delete_post_media( $post_id ) {
    $attachments = get_posts(
        array(
            'post_type'      => 'attachment',
            'posts_per_page' => -1,
            'post_status'    => 'any',
            'post_parent'    => $post_id,
        )
    );
    
    foreach ( $attachments as $attachment ) {
        wp_delete_attachment( $attachment->ID );
    }
}

add_action( 'before_delete_post', 'wpse_188427_delete_post_media' );

// Uncomment the following line if you also want to delete media when the post is trashed
// add_action( 'wp_trash_post', 'wpse_188427_delete_post_media' );

本文标签: pluginsDelete Associated Media Upon PagePost Deletion