WordPress WP-O-Matic Duplicate Posts Fix

Got a problem with WP-O-Matic creating duplicate posts? Or just WordPress in general. Me too.

I wrote a script to run on a regular basis so sort this problem out.Create wp-posts-cleanup.php in your wordpress/wp-config/plugins/ directory,

><?php
require_once(dirname(__FILE__) . '/../../wp-config.php');
require_once(dirname(__FILE__) . '/../../wp-includes/wp-db.php');

$wpdb->show_errors();

$dupes = $wpdb->get_results('select bad_rows.*
from wp_posts as bad_rows
inner join (
select post_title, MIN(id) as min_id
from wp_posts
group by post_title
having count(*) > 1
) as good_rows on good_rows.post_title = bad_rows.post_title
and good_rows.min_id <> bad_rows.id;
');

foreach ($dupes as $dupe) {
echo $dupe->post_title ."\n";
}

$wpdb->query('
delete bad_rows.*
from wp_posts as bad_rows
inner join (
select post_title, MIN(id) as min_id
from wp_posts
group by post_title
having count(*) > 1
) as good_rows on good_rows.post_title = bad_rows.post_title
and good_rows.min_id <> bad_rows.id;
');
?>
Running this will search and delete all your duplicate posts based on the post_title column.

*** Careful, it will delete the posts as soon as it runs! ***

Direct Download

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

36 thoughts on “WordPress WP-O-Matic Duplicate Posts Fix”

  1. Excellent, Thanks!
    Very nice script, perfect work-around until a new wp-o-matic release will have fixed this annoying bug hopefully 🙂

  2. For some odd reason, I just can’t figure out how to add this plug in to my directory – SOMEBODY HELP!!!!

    My email address = sid1 [at] gmx (dot) com

    Thanks….

  3. Hi Kiernan,

    I seem to be having the same problem as Sid.

    I cannot seem to figure out how to incoporate the fix. I see that I have to put a file in /wp-config/plugins/ directory but the only directories I see in my files are wp-admin,wp-content, and wp-includes.

    Any help you can share will be greatly appreciated.

    Thanks:), I sent you a direct email also

  4. I am using wordpress 2.7 with WP o-matic 1.0RC4-6. I don’t have wordpress/wp-config/plugins/ directory, so I created it in wordpress/wp-content/plugins/ directory but it din’t appear in the installed plugin area. How shall I activate and run the script? Can someone pls help?

  5. Many thanks for this fix. It doesn’t actually install as a plug-in but runs fine from browser, many thanks again, great work.

  6. Will this work on the latest WP version?
    I’m a little bit hesitant to try it if it messes up my site…

  7. Hi there mate, I was using basically the same query within phpMyAdmin:


    DELETE bad_rows.*
    from wp_posts as bad_rows
    inner join (
    select post_title, MIN(id) as min_id
    from wp_posts
    group by post_title
    having count(*) > 1
    ) as good_rows on good_rows.post_title = bad_rows.post_title
    and good_rows.min_id bad_rows.id

    Now one question: how would you modify the query to select only duplicated records on a given category?


    The following query selects all records from a given category:
    SELECT * FROM wp_posts
    LEFT JOIN wp_term_relationships ON(wp_posts.ID = wp_term_relationships.object_id)
    LEFT JOIN wp_term_taxonomy ON(wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
    WHERE
    (wp_term_taxonomy.term_id = 27)
    AND wp_term_taxonomy.taxonomy = 'category'

    How would you adjust that? Maybe with a nested query? Or?

    Thanks

  8. hello Keiran, first I wanna thanks for you nice plugin,i really save me many times, but since there’s WP 2.81 this plugin seems doesn’t work anymore. I think its because WP 2.8 has changes some db table structure ?

  9. hello Keiran, first I wanna thanks for you nice plugin,it really save me many times, but since there’s WP 2.81 this plugin seems doesn’t work anymore. I think its because WP 2.8 has changes some db table structure ?

  10. Ok, so if I have a prefix of say wp_pre in my database file would I have to change every instance of wp_posts to wp_pre_posts for it to run correctly?

  11. ok so i swapped out the wp_posts in the code provided and inserted wp_pre_posts since my prefix is wp_pre and ran the code..all it does is render the page as displaying just the text and not as a php program. i saved it as a php program…and uploaded it to the plugins directory.

    still no luck.

    suggestions?

  12. wow great script thanks for this works really great . i was using delete duplicate posts plugin , it was working fine before but it just freeze when there are near thousands posts and wp o matic creating lot of duplicate posts like 5-6 time of each post. Now no problem at all this takes a sec to do it. save lot of work Thanks again

  13. I’m on 3.0.5 and still the same problem from wp_insert_post.

    I didn’t take your solution, but it inspired me to delete duplicate posts, so thank you 😉

  14. for me unfortunately it’s not working ( way too large db at the moment I think ) … around 35K posts , with I think more than 5K duplicates maybe more … I try to run it from one vps on dreamhost and wasn’t working ( with max memory allowed ) …

  15. I think you’d have to write a small wrapper round it to split the results into chunks. Like a LIMIT 1000 on your SQL query.

Leave a Reply

Your email address will not be published. Required fields are marked *