如何从WordPress中删除所有图像

时间:2016-09-08 作者:ShadowKiller

I want to delete all the images. By all I means Images from Media Library as well as the from posts. I have 4000 images in wordpress upload directory and each image is attached to a post.

By \'detach\' and deleting a image permanently form Media Manger is not removing the image from the Post. :(

I searched and could not find any plugin. There is a plugin to search and delete all unattached / unused images. But I want to delete all the used images from Media Manager, Posts and Any Database References...

This is a great community and I hope I will able to fix this problem...

Thanks!Rohit

1 个回复
SO网友:Malisa

您可以尝试以下方法,我还没有测试过,所以请注意输入错误或错误:

$all_posts = get_posts(array(
    \'numberposts\' => - 1,
    \'post_status\' => \'any\',
    \'post_type\' => get_post_types(\'\', \'names\') ,
));

foreach($all_posts as $all_post) {
    delete_post_media($all_post->ID);
}

function delete_post_media($post_id)
{
    if (!isset($post_id)) return;
    elseif ($post_id == 0) return;
    elseif (is_array($post_id)) return;
    else {
            $attachments = get_posts(array(
                    \'post_type\' => \'attachment\',
                    \'posts_per_page\' => - 1,
                    \'post_status\' => \'any\',
                    \'post_parent\' => $post_id
            ));
            foreach($attachments as $attachment) {
                    if (false === wp_delete_attachment($attachment->ID)) {

                            // Log failure to delete attachment.

                    }
            }
    }
}

相关推荐

从wp.media.editor.附件获取所选图像URL到文本输入失败。我做错了什么?

我已将媒体按钮作为post meta field 在以下情况下将图像URL插入文本字段Insert Into Page 单击按钮。它一直工作到打开“媒体存储库”窗口、选择“图像”和“插入”为止,但没有向输入字段传递任何值。Expected result结果应该是URL和媒体ID插入为https://avalon.com/dev/wp-content/uploads/2021/01/scp7147prko31.jpg|12 但没有返回值。我可以手动输入URL并保存数据。我彻底扫描了我的代码,并尝试了许多ja