0

I have a hierarchical posts, when ever I delete the child posts its attachments go and link to deleted post's parent

How can I stop this?

2
  • how can I try anything unless I found any thing over internet? I googled for wordpress delete child post merges the attachment with parent post and look for wp settings to see if there is any setting to stop this.. Commented Apr 30, 2014 at 14:26
  • @riksof-zeeshan What do you mean by "attachments go and link to parent post"? Can you please elaborate.
    – A.Jesin
    Commented Apr 30, 2014 at 16:12

1 Answer 1

1

There's no way of doing this without "listening" for the responsible database query and altering it with the query filter, thanks to this line in wp_delete_post():

// Point all attachments to this post up one level
$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );

The following will override the query and set the post parent back to 0 (as opposed to inheriting the deleted items parent):

/**
 * Remove the parent fallback for attachments of children when they are
 * deleted.
 *
 * @link http://wordpress.stackexchange.com/q/142916/1685
 */
class WPSE_142916_Remove_Parent_Fallback {
    public static function init( $post_id ) {
        new self( $post_id );
    }

    public function __construct( $post_id ) {
        $this->post_parent = wp_get_post_parent_id( $post_id );
        $this->post_id = $post_id;

        add_action( 'delete_post', array( $this, 'un_hook' ) );
        add_filter( 'query',       array( $this, 'replace' ) );
    }

    public function replace( $query ) {
        global $wpdb;

        if ( $query === "UPDATE `$wpdb->posts` SET `post_parent` = $this->post_parent WHERE `post_parent` = $this->post_id AND `post_type` = 'attachment'" ) {
            $query = str_replace( "`post_parent` = $this->post_parent", '`post_parent` = 0', $query );
        }

        return $query;
    }

    public function un_hook() {
        remove_action( 'delete_post', array( $this, 'un_hook' ) );
        remove_filter( 'query',       array( $this, 'replace' ) );
    }
}

add_action( 'before_delete_post', array( 'WPSE_142916_Remove_Parent_Fallback', 'init' ) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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