15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[WordPress] メタデータのお掃除

Posted at

sql 叩いて直接 wp_posts から記事データ消したとかで、関連するメタデータが残ってることがあります。
それらを一括で削除するための SQL

remove_postmeta.sql
/* postmeta のお掃除 */
DELETE
 FROM wp_postmeta
 WHERE NOT EXISTS (
  SELECT 'x'
   FROM wp_posts
   WHERE wp_posts.ID = wp_postmeta.post_id
  );
/* comment のお掃除 */
DELETE
 FROM wp_comments
 WHERE NOT EXISTS (
  SELECT 'x'
   FROM wp_posts
   WHERE wp_posts.ID = wp_comments.comment_post_ID
  );
/* commentmeta のお掃除 */
DELETE
 FROM wp_commentmeta
 WHERE NOT EXISTS (
  SELECT 'x'
   FROM wp_comments
   WHERE wp_comments.comment_ID = wp_commentmeta.comment_id
  );
/* term_relationships のお掃除 */
DELETE
 FROM wp_term_relationships
 WHERE NOT EXISTS (
  SELECT 'x'
   FROM wp_posts
   WHERE wp_posts.ID = wp_term_relationships.object_id
  );
/* taxonomy に紐づけられた記事数の再カウント */
UPDATE wp_term_taxonomy
 SET count = (
  SELECT COUNT(object_id)
   FROM wp_term_relationships
   WHERE wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
  );
15
12
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?