admin管理员组文章数量:1405377
I am unable to delete attached files from their folders programmatically or even through the wordpress admin as the admin user. The attachment record deletes from the database but the file remains in the folder.
I can retrieve that file successfully using
get_attached_media( 'application', $post_id )
I can delete the record using
wp_delete_attachment($post_id, true)
But this doesn't delete the file. I tried feeding the confirmed path of the file to wp_delete_file(), no luck
I tried to manually unlink
function delete_unattached_attachments($id){
$attachments = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_parent' => $id,
));
if ($attachments) {
foreach ($attachments as $attachmentID){
$attachment_path = get_attached_file( $attachmentID->ID);
//Delete attachment from database only, not file
$delete_attachment = wp_delete_attachment($attachmentID->ID, true);
//Delete attachment file from disk
$delete_file = unlink($attachment_path);
}
}
}
I thought it was a capability at first but the admin can't even do it. I've tested on both localhost and live. Same deal. I check the read/write of the folder and I have delete priv.
The folders are json I add as application files for each post to hold chunks of data. I add them like so and they show correct parent in database.
$attachment = array(
'guid'=> $wp_upload_dir['url'] .'/'. basename( $file ),
'post_mime_type' => 'application/json',
'post_title' => $postTitle,
'post_content' => $postTitle,
'post_status' => 'inherit'
);
wp_insert_attachment($attachment, $file, $post_id);
Also the post parent of the attachment is a custom post type
本文标签: postsUnable to delete attached file from folder programmatically
版权声明:本文标题:posts - Unable to delete attached file from folder programmatically 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744873994a2629805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论