Blog / WordPress/ Prevent Duplicate Post Titles in WordPress: A Complete Implementation Guide

Prevent Duplicate Post Titles in WordPress: A Complete Implementation Guide

代码实现wordpress发布文章时检查标题是否重复

Overview

This feature prevents duplicate post titles in WordPress. When publishing or updating a post, the system checks if the title already exists among published posts. If a duplicate is found, publication is blocked, the post status is reverted to "draft," and an error message is displayed to the administrator.

Implementation Code

Add the following PHP code to the end of your active theme's functions.php file.

// Frontend AJAX check on title change
add_action('admin_print_footer_scripts', 'duplicate_titles_enqueue_scripts', 100);
function duplicate_titles_enqueue_scripts() {
?>

prepare(
        "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = %s AND post_title = %s AND ID != %d",
        $post_type, $title, $post_id
    );
    $results = $wpdb->get_results($query);
    if ($results) {
        echo 'This title already exists. Please choose a different one.';
    } else {
        echo 'Title is available.';
    }
    wp_die();
}

// Final validation on publish attempt
add_action('publish_post', 'duplicate_titles_final_check');
function duplicate_titles_final_check($post_id) {
    global $wpdb;
    $title = sanitize_text_field($_POST['post_title']);
    $query = $wpdb->prepare(
        "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_title = %s AND ID != %d",
        $title, $post_id
    );
    $results = $wpdb->get_results($query);
    if ($results) {
        $wpdb->update($wpdb->posts, array('post_status' => 'draft'), array('ID' => $post_id));
        $location = add_query_arg(array('message' => 10, 'duplicate_error' => 1), get_edit_post_link($post_id, 'url'));
        wp_redirect($location);
        exit;
    }
}

// Display admin notice for duplicate title error
add_action('admin_notices', 'duplicate_title_error_notice');
function duplicate_title_error_notice() {
    if (isset($_GET['duplicate_error']) && $_GET['duplicate_error'] == 1) {
        echo '

A published post with this title already exists. Please change the title or use a different post slug.

'; } } // Optional: Disable autosave (remove if not needed) add_action('wp_print_scripts', 'disable_autosave'); function disable_autosave() { wp_deregister_script('autosave'); }

How It Works

  1. Frontend AJAX Check: When the post title field changes, an AJAX request is sent to check for duplicates. A message (green/red) is displayed immediately.
  2. Backend Final Validation: When the "Publish" button is clicked, a final check runs via the publish_post hook. If a duplicate is found, the post status is set to "draft," and the user is redirected with an error.
  3. Error Notification: A red error notice appears in the admin area if a duplicate title is detected during publishing.

Important Notes

  • Only published posts are checked for duplicates. Drafts, scheduled posts, etc., are ignored.
  • The code includes an optional function to disable WordPress autosave. Remove the last two functions if you wish to keep autosave enabled.
  • If you use post slugs (permalinks), you can still publish posts with duplicate titles by using a unique slug.
  • Always back up your functions.php file before making changes.

Post a Comment

Your email will not be published. Required fields are marked with *.