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!

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

This is a great way to inform authors that something is wrong with their post. 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!

How to Put an Image Before the Post Title in WordPress

Sometimes you might want to place an icon or an image before the post title. This might be a different image for each post, depending on your requirements. Here's a simple step by step guide with screenshots on how to accomplish this. Step 1: Upload … [Continue reading]

How to Link to a Draft in WordPress with Pretty Permalinks

The "Insert/Edit" link button in WordPress is a great tool for quickly referencing your own content. You can search by keyword and it automatically fills in the permalink. However, it has one major limitation - you can't link to a draft post. This is … [Continue reading]

Change the Header Image Per Page in WordPress

Many WordPress themes allow you to set a header image to display on top of the page. This is done via the GUI in the theme customization options like this: But , this applies to ALL pages of the site. By default, there's no way to choose which … [Continue reading]

How to Remove Links in Posts with WordPress

Imagine you have an article submission site, where readers can generate their own content. But you want to make sure that it's not abused with spam links. Let's say you need to disallow all links in your content and not allow users to submit them at … [Continue reading]

How to Manually Switch a WordPress Theme via FTP

If you're staring at a broken WordPress theme and can't even access your administration dashboard, don't panic! This step by step tutorial will show you how to switch to another theme manually using FTP. But first, let me break my theme: Breaking … [Continue reading]

How to Display Custom Error Messages in WordPress Admin

There are many ways to display custom error messages in WordPress. You must have often seen WordPress itself show various error messages in the admin area. Nags to update to the latest release, for example. Various plugins also inject their custom … [Continue reading]

How to Remove “Comments are Closed” From WordPress Pages

When you create a static landing page in WordPress, you'll probably want to remove "comments are closed" from showing up underneath your content. Since version 4.3, comments have been disabled by default for pages. But why then, do we continue to … [Continue reading]

Show a List of Child Pages without Altering Templates

If you have a list of child pages, you may want to show them whenever someone visits the parent page. Several solutions on the net involve making modifications to your theme template, or even creating a new template page. Some even recommend … [Continue reading]

How to Set the Default Facebook Image in WordPress

This article will show you how to set a default Facebook image for your WordPress post. When you share it on your own, you might be able to choose the image from the options, or upload a new one. But you have no control over how it might when someone … [Continue reading]