Skip to main content

Delete How to delete duplicate recordrecords in wp_postmeta database table?

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 77 characters in body
Source Link
LeonardShelby
  • 165
  • 1
  • 4
  • 13

Some of my posts have two values for a meta key called cb_full_width_post. I want to only keep the records that have a meta value of nosidebar-narrow. This means I need to

  1. Identify records in wp_postmeta that have the same post ID and also have an entry for the meta key cb_full_width_post
  2. Delete all records identified in step 1 that do not have meta value of nosidebar-narrow

So far, I have come up with this SQL to identify posts with two or more values for the same meta key cb_full_width_post:

SELECT post_id, meta_key, count(*)
FROM wp_postmeta
WHERE meta_key = 'cb_full_width_post'
GROUP BY post_id, meta_key
HAVING COUNT(*) > 1

This returns a result set of wp_postmeta records having more than one value for cb_full_width_post.
I'm stuck on the next step. I need to use the result set from the above query, which gives me the post IDs that I need to target. I need to find those post IDs in wp_postmeta, and delete all records where meta_value != 'nosidebar-narrow'

Edit: some visual info

post_id: 1

meta_key: cb_full_width_post

meta_value: Good

post_id: 1

meta_key: cb_full_width_post

meta_value: Bad

post_id:. 2

meta_key: cb_full_width_post

meta_value:. Bad

Record #1
post_id:     1  
meta_key:    cb_full_width_post   
meta_value:  Good

Record #2
post_id:     1
meta_key:    cb_full_width_post
meta_value:  Bad_if_duplicate

Record #3
post_id:     2
meta_key:    cb_full_width_post
meta_value:  Bad_if_duplicate

I only want to delete the record in wp_postmeta withRecord #2, because it has meta_value "Bad_if_duplicate" and because it is a duplicate (same post ID 1 and meta_value "Bad"as Record #1). Even though the record with post ID 2Record #3 has meta_value "Bad""Bad_if_duplicate", I don't want to delete it, because that is its only meta_value, i. ( The record with post_id 2 doese. it's not have a duplicate ).

Some of my posts have two values for a meta key called cb_full_width_post. I want to only keep the records that have a meta value of nosidebar-narrow. This means I need to

  1. Identify records in wp_postmeta that have the same post ID and also have an entry for the meta key cb_full_width_post
  2. Delete all records identified in step 1 that do not have meta value of nosidebar-narrow

So far, I have come up with this SQL to identify posts with two or more values for the same meta key cb_full_width_post:

SELECT post_id, meta_key, count(*)
FROM wp_postmeta
WHERE meta_key = 'cb_full_width_post'
GROUP BY post_id, meta_key
HAVING COUNT(*) > 1

This returns a result set of wp_postmeta records having more than one value for cb_full_width_post.
I'm stuck on the next step. I need to use the result set from the above query, which gives me the post IDs that I need to target. I need to find those post IDs in wp_postmeta, and delete all records where meta_value != 'nosidebar-narrow'

Edit: some visual info

post_id: 1

meta_key: cb_full_width_post

meta_value: Good

post_id: 1

meta_key: cb_full_width_post

meta_value: Bad

post_id:. 2

meta_key: cb_full_width_post

meta_value:. Bad

I only want to delete the record in wp_postmeta with post ID 1 and meta_value "Bad". Even though the record with post ID 2 has meta_value "Bad", I don't want to delete it, because that is its only meta_value. ( The record with post_id 2 does not have a duplicate ).

Some of my posts have two values for a meta key called cb_full_width_post. I want to only keep the records that have a meta value of nosidebar-narrow. This means I need to

  1. Identify records in wp_postmeta that have the same post ID and also have an entry for the meta key cb_full_width_post
  2. Delete all records identified in step 1 that do not have meta value of nosidebar-narrow

So far, I have come up with this SQL to identify posts with two or more values for the same meta key cb_full_width_post:

SELECT post_id, meta_key, count(*)
FROM wp_postmeta
WHERE meta_key = 'cb_full_width_post'
GROUP BY post_id, meta_key
HAVING COUNT(*) > 1

This returns a result set of wp_postmeta records having more than one value for cb_full_width_post.
I'm stuck on the next step. I need to use the result set from the above query, which gives me the post IDs that I need to target. I need to find those post IDs in wp_postmeta, and delete all records where meta_value != 'nosidebar-narrow'

Edit: some visual info

Record #1
post_id:     1  
meta_key:    cb_full_width_post   
meta_value:  Good

Record #2
post_id:     1
meta_key:    cb_full_width_post
meta_value:  Bad_if_duplicate

Record #3
post_id:     2
meta_key:    cb_full_width_post
meta_value:  Bad_if_duplicate

I only want to delete Record #2, because it has meta_value "Bad_if_duplicate" and because it is a duplicate (same post ID as Record #1). Even though Record #3 has meta_value "Bad_if_duplicate", I don't want to delete it, because that is its only meta_value, i.e. it's not a duplicate.

added 510 characters in body
Source Link
LeonardShelby
  • 165
  • 1
  • 4
  • 13

Some of my posts have two values for a meta key called cb_full_width_post. I want to only keep the records that have a meta value of nosidebar-narrow. This means I need to

  1. Identify records in wp_postmeta that have the same post ID and also have an entry for the meta key cb_full_width_post
  2. Delete all records identified in step 1 that do not have meta value of nosidebar-narrow

So far, I have come up with this SQL to identify posts with two or more values for the same meta key cb_full_width_post:

SELECT post_id, meta_key, count(*)
FROM wp_postmeta
WHERE meta_key = 'cb_full_width_post'
GROUP BY post_id, meta_key
HAVING COUNT(*) > 1

This returns a result set of wp_postmeta records having more than one value for cb_full_width_post.
I'm stuck on the next step. I need to use the result set from the above query, which gives me the post IDs that I need to target. I need to find those post IDs in wp_postmeta, and delete all records where meta_value != 'nosidebar-narrow'

Edit: some visual info

post_id: 1

meta_key: cb_full_width_post

meta_value: Good

post_id: 1

meta_key: cb_full_width_post

meta_value: Bad

post_id:. 2

meta_key: cb_full_width_post

meta_value:. Bad

I only want to delete the record in wp_postmeta with post ID 1 and meta_value "Bad". Even though the record with post ID 2 has meta_value "Bad", I don't want to delete it, because that is its only meta_value. ( The record with post_id 2 does not have a duplicate ).

Some of my posts have two values for a meta key called cb_full_width_post. I want to only keep the records that have a meta value of nosidebar-narrow. This means I need to

  1. Identify records in wp_postmeta that have the same post ID and also have an entry for the meta key cb_full_width_post
  2. Delete all records identified in step 1 that do not have meta value of nosidebar-narrow

So far, I have come up with this SQL to identify posts with two or more values for the same meta key cb_full_width_post:

SELECT post_id, meta_key, count(*)
FROM wp_postmeta
WHERE meta_key = 'cb_full_width_post'
GROUP BY post_id, meta_key
HAVING COUNT(*) > 1

This returns a result set of wp_postmeta records having more than one value for cb_full_width_post.
I'm stuck on the next step. I need to use the result set from the above query, which gives me the post IDs that I need to target. I need to find those post IDs in wp_postmeta, and delete all records where meta_value != 'nosidebar-narrow'

Some of my posts have two values for a meta key called cb_full_width_post. I want to only keep the records that have a meta value of nosidebar-narrow. This means I need to

  1. Identify records in wp_postmeta that have the same post ID and also have an entry for the meta key cb_full_width_post
  2. Delete all records identified in step 1 that do not have meta value of nosidebar-narrow

So far, I have come up with this SQL to identify posts with two or more values for the same meta key cb_full_width_post:

SELECT post_id, meta_key, count(*)
FROM wp_postmeta
WHERE meta_key = 'cb_full_width_post'
GROUP BY post_id, meta_key
HAVING COUNT(*) > 1

This returns a result set of wp_postmeta records having more than one value for cb_full_width_post.
I'm stuck on the next step. I need to use the result set from the above query, which gives me the post IDs that I need to target. I need to find those post IDs in wp_postmeta, and delete all records where meta_value != 'nosidebar-narrow'

Edit: some visual info

post_id: 1

meta_key: cb_full_width_post

meta_value: Good

post_id: 1

meta_key: cb_full_width_post

meta_value: Bad

post_id:. 2

meta_key: cb_full_width_post

meta_value:. Bad

I only want to delete the record in wp_postmeta with post ID 1 and meta_value "Bad". Even though the record with post ID 2 has meta_value "Bad", I don't want to delete it, because that is its only meta_value. ( The record with post_id 2 does not have a duplicate ).

Source Link
LeonardShelby
  • 165
  • 1
  • 4
  • 13
Loading