WordPress – How to Prevent Duplicate Post Titles

If you have a lot of people contributing content to your blog, you might accidentally have two posts with the same title. By default, WordPress doesn’t raise any red flags when that happens. It’ll generate a unique URL by appending a number (1, 2, etc.) after the original URL and so prevent any conflict. But it’s a terrible idea to have duplicate titles like this. Not only will they contend with each other, they might both be devalued!

Note: This post is about preventing new articles from having the same title as previous posts. Click here to fix duplicate <title> tags in WordPress.

While there are several plugins that prevent duplicate titles from being added, I’ll show you how to ban duplicate post titles without plugins.

Disallow Duplicate Post Titles – Add Code

In your functions.php or wherever you place your custom PHP code, add the following:

function disallow_posts_with_same_title($messages) {
    global $post;
    global $wpdb ;
    $title = $post->post_title;
    $post_id = $post->ID ;
    $wtitlequery = "SELECT post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_title = '{$title}' AND ID != {$post_id} " ;
 
    $wresults = $wpdb->get_results( $wtitlequery) ;
 
    if ( $wresults ) {
        $error_message = 'This title is already used. Please choose another';
        add_settings_error('post_has_links', '', $error_message, 'error');
        settings_errors( 'post_has_links' );
        $post->post_status = 'draft';
        wp_update_post($post);
        return;
    }
    return $messages;

}
add_action('post_updated_messages', 'disallow_posts_with_same_title');

If you don’t know how to add code to your functions.php, read the following earlier tutorial on adding code snippets to WordPress.

This code works in a very simple manner. It provides an action hook that fires whenever the post is updated – either saved as a draft, pending, or published. Then it runs an SQL query against the WordPress database searching for similar titles but with a different Post ID compared to the current one. If any results are returned, we know that it’s a duplicate post title.

We display the error message in the post editor using the graceful message framework provided by WordPress:

Duplicate Post Titles Error Message
Duplicate Post Titles Error Message

This is a great way to inform authors that something is wrong with their posts. Instead of an unfriendly error message that leads them to a blank page, we display it right at the top of the post and revert the post to a “draft” status, thus preventing it from being published.

So there’s no need for a separate WordPress plugin to prevent duplicate post titles on your WordPress website. Simply add the above code and you’re done!

About Bhagwad Park

I've been writing about web hosting and WordPress tutorials since 2008. I also create tutorials on Linux server administration, and have a ton of experience with web hosting products. Contact me via e-mail!

Comments

  1. How can one implement a system to prevent duplicate posts by mail
    So this will prevent the blog post being inserted/committed published in the database and in addition also prevent sharing of it. Discarding the insert or sending it to trash is perfect

    We have multiple automated systems which post blogs by mail to itradesignals.com and it will be fabtastic to prevent this at the website database level -as the fix elsewhere is going to be a long long one…

    thanks any feedback

    Reply

    • WPTweaks Admin says

      Hmm…I’ll have to look into it. Probably something on a similar note as the above solution, but with a different hook.

      Reply

  2. Erik Molenaar says

    Thanks for this great snippet! It’s compact and easy.

    Reply

  3. Its really a great help to coders who not prefer to install plugins. This code is awesome and works perfectly.

    Reply

  4. Hey Bhagwad,

    Thanks for the simple and easy snippet, I really like it! I had two questions about modifying this snippet:

    1. Is there is a way to add to the text string a link to the duplicated post/s? So for example, instead of the wording above, it will say: “This Title is already used, view here. Please use another” and the “here” will be a hyperlink to that other post?

    2. Once I view it the other snippet, I might anyway want to go ahead and use the duplicated title name. Is there a way to stick into the snippet “use anyway” button, that will bypass the error and let me submit?

    Thanks so much!
    Sim

    Reply

  5. Is there any way you can help me out with this?

    Reply

  6. Hi
    Thanks for the solution, but for reason it is not working.
    my wordpress version is 5.9, tried adding the code directly to theme functions

    Reply

  7. Is there a way to force this code to be implemented on the whole site? I have a plugin that auto-creates posts and when I use that, the duplicate posts are still being published. However, if I manually add a new post with a duplicate tile, the code works great.

    Reply

  8. hi
    thanks for the solution but its not working on wordpress 6.0.
    can u pls help me out. i added it directly to my functions file

    Reply

  9. How would it be using postmeta meta_key, meta_value.

    Reply

Speak Your Mind

*

WP-Tweaks