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?
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' ) );
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..