Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using this function to make sure there is no duplicate post or pages with the same title. But the problem is when i add a new post with the same title as a old post the new post that I'm adding is removed and it keeps the old post, and i need it the other way around where the old post is removed and new post that I'm adding is kept.

function clearDuplicatePosts(){
  global $wpdb;
  $prefix = $wpdb->prefix;

  $wpdb->query("DELETE bad_rows . * FROM ".$prefix."posts AS bad_rows INNER JOIN (
    SELECT ".$prefix."posts.post_title, MIN( ".$prefix."posts.ID ) AS min_id
    FROM ".$prefix."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 )");
}
add_action('publish_post', 'clearDuplicatePosts');
add_action('publish_page', 'clearDuplicatePosts');
share|improve this question

closed as too localized by toscho Jul 13 '12 at 19:52

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

try to add something with the date when the post was created. If date(post1)>date(post2) delete post2.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.