如何在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中。
如果要删除,后果自负,可以先尝试执行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中的所有记录,对我来说似乎很好。为什么它可能对您不起作用,对我来说还是个谜。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句