识别帖子被删除的附件元数据

Identify attachement meta data whose posts were deleted

一些 wordpress 帖子几年前就被删除了,但它们的相关媒体仍在文件系统中。

有没有办法识别帖子被删除的附件元数据,然后从文件系统中删除元数据和文件?

如果您有权访问数据库,最简单的方法是 运行 一个 SQL 查询。假设您的数据库前缀是 wp_,您可以 SELECT 通过 post_type 搜索缺少父级的媒体附件,其中 post_parent 不在 wp_posts 中table.

SELECT * 
FROM wp_posts 
WHERE 
  -- the attachments
  post_type = 'attachment' 
  AND (
    -- ignore anything without a parent
    post_parent != 0 
    -- filter on anything that has a parent that does not exist
    AND post_parent NOT IN (SELECT ID FROM wp_posts)
  )

SELECT * 替换为 DELETE 以从数据库中删除这些帖子。如果您没有级联删除,您可能还需要检查 wp_postmeta table 是否有任何 post_id 不与 wp_posts.ID 对齐的条目。