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

标签 php mysql wordpress

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

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

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

最佳答案

您遇到的问题是您正在为各种事情撤回“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 中删除与特色图片没有关联的所有记录,它对我来说似乎工作得很好。为什么它可能对您不起作用,这对我来说是个谜。

关于php - 如何通过mysql删除wordpress中没有特色图片的帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47656714/

相关文章:

php - 如何修复 "Cannot add or update a child row: a foreign key constraint fails"

php - 使用三元运算符进行引用赋值

php - symfony + Doctrine 帮助

mysql 如何加入表并做一些数学运算

html - Wordpress 中的占位符图标 Contact Form 7

php - 在 Zend 中使用多语言路由器

php - 如何在sql上获取我以前的值?

mysql - 连接 3 个表,查询无响应

javascript - Wordpress ajax 返回 html

Wordpress SlideDeck2 插件与 Role Scoper 插件冲突