admin管理员组文章数量:1122832
I'm looking for a safe and fast way to delete all of one custom post type's posts. Using get_posts()
and wp_delete_post()
for each returned post does not work; it's not fast enough due to the sheer amount of database queries involved (timeout error).
Preferably, I'm looking for a single database query to run that deletes all posts that are of a custom post type. Any thoughts?
I'm looking for a safe and fast way to delete all of one custom post type's posts. Using get_posts()
and wp_delete_post()
for each returned post does not work; it's not fast enough due to the sheer amount of database queries involved (timeout error).
Preferably, I'm looking for a single database query to run that deletes all posts that are of a custom post type. Any thoughts?
Share Improve this question asked Nov 13, 2015 at 21:29 Marcus McLeanMarcus McLean 3811 gold badge3 silver badges4 bronze badges 9 | Show 4 more comments4 Answers
Reset to default 42You can delete all post via $wpdb
DELETE FROM wp_posts WHERE post_type='post_type';
DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT id FROM wp_posts);
DELETE FROM wp_term_relationships WHERE object_id NOT IN (SELECT id FROM wp_posts)
or use this query replace it with {{your CPT}} with your Custom Post Type
DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b
ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c
ON (a.ID = c.post_id)
WHERE a.post_type = '{{your CPT}}';
This can now be done with the WordPress CLI using wp post delete. Once the CLI is installed, the following terminal command (while in your site's root directory) will delete all posts of type mycustomtype
:
wp post delete $(wp post list --post_type='mycustomtype' --format=ids)
No raw SQL (*shudder*), no worrying about timeouts, and it's fast. For example, I just deleted ~2500 posts in less than two minutes.
To skip Trash and delete it completely use --force
You can delete all posts of a custom post type in various methods but here I am gonna show you how to do this without using SQL query. Here, for example, our post type is product
$allposts= get_posts( array('post_type'=>'product','numberposts'=>-1) );
foreach ($allposts as $eachpost) {
wp_delete_post( $eachpost->ID, true );
}
See full tutorial Reference Here
If you have prefixed your CPT post type and your CPT taxonomies with eg. 'abc_my_custom_post_type' and 'abc_my_taxonomy' then it's trivial to remove everything from db with two queries:
DELETE a,b
FROM $wpdb->posts a
LEFT JOIN $wpdb->postmeta b ON a.ID = b.post_id
WHERE a.post_type LIKE 'abc_%';
DELETE a,b,c
FROM $wpdb->term_taxonomy a
LEFT JOIN $wpdb->term_relationships b ON a.term_taxonomy_id = b.term_taxonomy_id
LEFT JOIN $wpdb->terms c ON a.term_id = c.term_id
WHERE a.taxonomy LIKE 'abc_%'
本文标签: databaseDelete all posts of a custom post typeefficiently
版权声明:本文标题:database - Delete all posts of a custom post type—efficiently 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736305536a1932626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wpdb
class then. Preferred method and "the WP way" for working directly with the DB. Let us know if you need help with that query. I can post a full answer later if needed codex.wordpress.org/Class_Reference/wpdb – jdm2112 Commented Nov 13, 2015 at 22:14'fields' => 'ids',
inget_posts
to get only post ID. This is all you need and it will significantely speed up your query – Pieter Goosen Commented Nov 14, 2015 at 3:09wp_cron()
– s_ha_dum Commented Nov 14, 2015 at 4:08