【问题标题】:How to delete posts without featured images in wordpress via mysql?如何通过mysql删除wordpress中没有特色图片的帖子?
【发布时间】:2017-12-05 15:04:09
【问题描述】:

如何通过 mysql 在 wordpress 中删除没有特色图片的帖子?

我尝试了此代码,但它也删除了所有包含特色图片的帖子。我只想删除没有特色图片的帖子。

DELETE FROM wp_posts WHERE ID not in (select post_id as p from wp_postmeta where meta_key like "_wp_attached_file")

【问题讨论】:

  • 我也试图找到这个任务的插件,但找不到。

标签: php mysql wordpress


【解决方案1】:

您遇到的问题是您正在为各种事情撤回“post_id”。

您想要的 meta_key 值是“_thumbnail_id”,它会将父帖子存储在 post_id 中。

DELETE 后果自负,您可以先尝试使用 SELECT 而不是 DELETE,以确保这样做是您真正想要的:

DELETE FROM wp_posts WHERE ID not in (select post_id as p from wp_postmeta where meta_key like "_thumbnail_id"); 

更新:这是我的验证测试。

创建了一个新帖子 (7161),然后添加了一张特色图片。

mysql> select * from wp_postmeta where post_id IN(7161,7162);
+---------+---------+---------------+---------------------+
| meta_id | post_id | meta_key      | meta_value          |
+---------+---------+---------------+---------------------+
|   12533 |    7161 | _edit_lock    | 1512581739:1        |
|   12534 |    7161 | _edit_last    | 1                   |
|   12549 |    7161 | _thumbnail_id | 7115                |
|   12543 |    7161 | CODE1         |                     |
|   12544 |    7161 | _CODE1        | field_57e4293a9238c |
|   12545 |    7161 | CODE3         |                     |
|   12546 |    7161 | _CODE3        | field_57e4297c9238d |
|   12547 |    7161 | CODE2         |                     |
|   12548 |    7161 | _CODE2        | field_57e42ceb508ff |

创建了 wp_posts 表的克隆

create table wp_posts_test like wp_posts;
  Query OK, 0 rows affected (0.01 sec)

并使用来自 wp_posts 的实时数据加载它。

insert into wp_posts_test select * from wp_posts;
  Query OK, 6938 rows affected (0.30 sec)

然后运行我的测试查询以删除没有特色图片的任何内容:

delete from wp_posts_test WHERE ID NOT IN (select post_id from wp_postmeta where meta_key="_thumbnail_id"); 
   Query OK, 6937 rows affected (0.37 sec)

查了表,只有一条记录,就是我想保留的帖子:

select * from wp_posts_test \G

*************************** 1. row ***************************
                   ID: 7161
          post_author: 1
            post_date: 2017-12-06 09:29:20
        post_date_gmt: 2017-12-06 17:29:20
         post_content: This is just a test post to see if the featured image OR the post is nuked using a mysql hack.   Standby for destruction. 
           post_title: Feature Image Test Post
        post_category: 0
         post_excerpt: 
          post_status: publish
       comment_status: open
          ping_status: open
        post_password: 
            post_name: feature-image-test-post
              to_ping: 
               pinged: 
        post_modified: 2017-12-06 09:35:18
    post_modified_gmt: 2017-12-06 17:35:18 
post_content_filtered: 
          post_parent: 0
                 guid: http://blog.daviddemartini.com/?p=7161
           menu_order: 0
            post_type: post
       post_mime_type: 
        comment_count: 0

一组中的 1 行(0.00 秒)

如果您想从 wp_posts 中删除与特色图像没有关联的所有记录,它看起来对我来说非常有效。为什么它可能不适合你,对我来说是个谜。

【讨论】:

  • 您好,感谢您的帮助,对不起,我出城了一些紧急工作,所以不要更新你。我现在回来了,只是测试这段代码,这会从帖子中删除所有缩略图(特色图像)。你能调查一下吗?再次感谢
  • 当我对此进行测试时,它找到了所有没有缩略图类型元记录的帖子(这表明存在关联的特色图像)。这不是目标,还是有其他目标?
  • 我希望删除没有特色图片的帖子。但是使用您的代码,特色图片本身会从拥有它的帖子中删除。
  • 我的测试通过创建 wp_posts 的副本作为 wp_posts_tests 并运行查询来工作。我只有一篇带有特色图片的帖子,在运行查询后,wp_posts 中只剩下那个帖子 ID。如果这对您不起作用,那么我不确定原因是什么,但它在最新版本的 WordPress 上对我来说工作正常。
  • 感谢您的时间和帮助,我认为问题出在我的最后,因为我此时无法使用 phpmyadmin,所以我使用 wordpress 插件(SQL 执行器)和结果原始查询:从 avwp_posts 删除ID 不在(从 avwp_postmeta 中选择 post_id 作为 p,其中 meta_key 像“_thumbnail_id”); 3449 行受到影响,所以我想我使用插件可能是这个原因。再次感谢..
【解决方案2】:

DELETE FROM wp_posts WHERE ID not in (select post_id as p from wp_postmeta where meta_key like "_thumbnail_id"); 它只是从帖子中删除了所有未来的图像。

【讨论】:

    猜你喜欢
    • 2018-04-16
    • 2013-11-08
    • 2013-10-04
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    相关资源
    最近更新 更多