【问题标题】:Wordpress - Get all posts without image?Wordpress - 获取所有没有图片的帖子?
【发布时间】:2013-10-03 09:59:45
【问题描述】:

有没有办法从 Sql 中找到所有没有任何关联图像(内联或附件)的帖子?

或者任何人都可以向我解释 wordpress 如何存储图像,因为我不太清楚。我发现它们可以内联在文本中,也可以使用 meta_key = "_wp_attached_file" 存储在 wp_postmeta 中,或者使用 post_type = "attachment" 存储在 wp_posts 中。

这样对吗?

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

  • 帖子作为对象返回。在该对象中是特色图像 ID,因为图像也作为其自己的帖子存储。只需返回对象键减去特色图像。

标签: mysql image wordpress


【解决方案1】:

Wordpress 通过创建attachment post 来管理媒体(图像、文本文档等),以保存有关该媒体的信息以及它与其他帖子/帖子的关系(如果有的话)。

要检索没有附加任何图像的所有帖子,您可以执行如下查询:

select * from wp_posts where id not in (select post_id as p from wp_postmeta where meta_key like "_thumbnail_id")

【讨论】:

    【解决方案2】:

    谢谢,我已经添加:

    AND ID not in (select post_id as p from wp_postmeta where meta_key like "_wp_attached_file")
    

    但它仍然返回太多结果。

    我已经看到在我的结果的 post_content 中,我有一些图像声明如下:

    <img src="/public/Username/filename.jpg">
    

    并且该查询不排除该帖子。我得到了一个很好的结果:

    SELECT DISTINCT(p.ID), p.post_title, p.post_content FROM `wp_posts` p
    LEFT JOIN wp_posts im ON p.ID = im.post_parent AND im.post_type = "attachment" 
    WHERE p.post_status ='publish' 
        AND p.post_type = "post" 
        AND im.ID IS NULL
        AND p.post_content NOT REGEXP 'src=".*"' 
    

    【讨论】:

    • 在回答这个问题时需要考虑一个重要的区别。在此示例中,“没有图像”将排除在帖子正文中具有图像标签的任何帖子。另一种方法是排除与 Wordpress 附件相关的帖子。因为有两种方式都可以选择,所以可能需要考虑这两种情况才能获得完整的答案。投票给这个。
    猜你喜欢
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多