这就是你要找的吗?
e.g. Remove all image attachments from a post
//get all image attachments
$attachments = get_children(
array(
\'post_parent\' => $post->ID,
\'post_mime_type\' => \'image\',
\'post_type\' => \'attachment\'
)
);
//loop through the array
if( !empty( $attachments ) ){
foreach( $attachments as $attachment ){
// Update the post into the database
wp_update_post( array(
\'ID\' => $attachment->ID,
\'post_parent\' => 0
)
);
}
}
但是,请注意
caution 使用wp\\u update\\u post时。
Alternate method using $wpdb
//replace this with the above inside the foreach block;
global $wpdb;
$wpdb->query(
"
UPDATE $wpdb->posts
SET post_parent = 0
WHERE ID = $attachment->id
AND post_type = \'attachment\'
"
);