• Resolved Marc Toensing

    (@marcdk)


    Hi!

    My plugin accidentally added several hundred custom fields to each post with the same key and the same value.

    It looks like this:

    my_key = 1234
    my_key = 1234
    my_key = 1234
    my_key = 1234
    my_key = 1234
    my_key = 1234
    my_key = 1234
    my_key = 4321
    my_key = 4321
    my_key = 4321
    my_key = 4321
    my_key = 4321

    And so on. Is there an SQL delete statement which checks for the value and then delete all keys with the same content? The result should look like this:

    my_key = 4321
    my_key = 1234

    An idea? Around 30.000 posts have this probem. =/

Viewing 3 replies - 1 through 3 (of 3 total)
  • You should be able to do this with a combination of 2 queries.

    BACK UP THE DATABASE FIRST!!

    The first query creates a temporary table containing the data for posts with duplicates:

    CREATE TABLE temp_tbl SELECT meta_id, post_id, meta_key, meta_value, count( post_id ) AS count_id
    FROM wp-postmeta
    GROUP BY post_id, meta_key, meta_value
    HAVING count_id >1;

    The second query then deletes rows from the wp-postmeta table where the post_id, meta_key, and meta_value match the temporary table:

    DELETE  pm
    FROM wp-postmeta pm, temp_tbl tt
    WHERE tt.post_id = pm.post_id
    AND tt.meta_key = pm.meta_key
    AND tt.meta_value = pm.meta_value
    AND pm.meta_id <> tt.meta_id

    Drop temp_tbl once you are done.

    If you encounter a problem because you are deleting too many rows at once, use a LIMIT clause on the first query to limit the number of entries in temp_tbl. Then, after running the second query, drop temp_tbl and rerun again until all rows are deleted.

    Thread Starter Marc Toensing

    (@marcdk)

    Thank you very much for your detailed answer!

    If your problem has been solved, please use the dropdown on the right to mark this topic ‘Resolved’ so that anyone else with a similar question can see that there is a solution.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to delete multiple post meta keys with equal values?’ is closed to new replies.