如何在wordpress中通过mysql删除没有精选图片的帖子?
我尝试了此代码但删除了所有包含精选图片的帖子。我只想删除没有精选图片的帖子。
member_tag
-------------
member_id | tag_id | tagActive
1 | 1 | 1
1 | 2 | 1
1 | 3 | 0
1 | 4 | 1
2 | 1 | 1
2 | 2 | 1
2 | 3 | 1
2 | 4 | 1
答案 0 :(得分:1)
您遇到的问题是您正在撤回“post_id”'各种各样的事情。
您想要的meta_key值是" _thumbnail_id"将post post存储在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中没有与特色图片相关联的所有记录,它看起来对我来说非常好。为什么它可能不适合你,对我来说是一个谜。
答案 1 :(得分:0)
DELETE FROM wp_posts WHERE ID not in (select post_id as p from wp_postmeta where meta_key like "_thumbnail_id"); 它只是从帖子中删除了所有未来的图片。