kieranbarnes Independent PHP, WordPress & CubeCart Programmer

WordPress WP-O-Matic Duplicate Posts Fix

Posted on July 5, 2008

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


Related posts

  1. WP-O-Matic Bug Fixes Round Up
    I've run into quite a few issues in the latest 1.0RC3-7 release of WP-O-Matic. Here's...
  2. Strictly Auto Tags Plugin & Custom Posts
    The Strictly Auto Tags plugin for WordPress does a fantastic job at automatically tagging content so...
  3. How to move WordPress directory or server – properly
    I ran into an annoying, but easily avoided problem the other day. When moving a...
  4. WP-o-Matic fix for WordPress 2.5
    WP-o-Matic makes autoblogging a easy by automatically creating posts from the RSS/Atom feeds you give...
  5. WP-o-Matic Upgrade Fix
    So I rolled out some WP-o-Matic upgrades recently. I was all excited that version 1.0RC3-7...

Posted by Kieran


Tagged as: , , Leave a comment
Comments (31) Trackbacks (3)
  1. Excellent, Thanks!
    Very nice script, perfect work-around until a new wp-o-matic release will have fixed this annoying bug hopefully :-)

  2. Thanks!
    I was Looking for it

  3. 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….

  4. Very, very, very good plugin safelife safetime safeall…

    thank you so much

  5. 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

  6. 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?

  7. wow!!!! :) ))) thank you

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

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

  10. Works great!!!

    FYI – You dont need to go to the plugins page in admin to use this. Just access the url where it’s located:

    http://bloke.org/wp-content/plugins/wp-o-matic-cleanup.php

    Thanks for the great script. Could you add the feature to prevent duplicate posts from being published?

  11. I added this to wp-content/plugins. How do i run it? :-)

  12. Be sure to check first if your db has wp_posts and not wp_123_posts.
    Check the prefix of the config file.

  13. What this tools is auto run and delete duplicate content after upload to wp-content/plugins/ ?

  14. Nice work! Saved a big days work.

  15. 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

  16. 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 ?

  17. 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 ?

  18. Hey there and thanks for the plugin. Will this fix the problem where the suffix -2 is added to dupe posts?

    In other words. Usualy wp-o-matic will have a post called:

    http://blog.com/New-template/
    http://blog.com/New-template-2/
    http://blog.com/New-template-3/

    Will this tool remove the 2 posts with the suffix -2 and -3?

  19. Does this work on the latest version of WordPress? 2.8.4 I think.

  20. 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?

  21. Ok. I saved this script in a .php file, placed it within the plugins directory, allowed 777 mod file permissions and it won’t work. Any advice?

  22. 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?

  23. 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

  24. 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 ;)

  25. how do i run this automatically?? this is nice plugin.. very well work done

  26. With a cron!

  27. 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 ) …

  28. 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.

  29. That was a wonderful code. Thank you very much. 18000 posts in data vase and it was quick and efficient…

    Kyle


Leave a comment

(required)