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');
