<?php
/**
- Plugin Name: Unique Post Title Checker
- Description: This plugin checks if a post with the same title already exists before publishing it. If a duplicate title is found, the post is set to draft status and a notice is added.
- Version: 1.0
- Author: Your Name
- Author URI: http://yourwebsite.com
- License: GPL2
*/
function unique_post_title_checker($data, $postarr) {
// Skip this check if the post is being auto-drafted (like when the user first visits the new post page).
if ($data[‘post_status’] === ‘auto-draft’) {
return $data;
}
// Search for an existing post with the same title.
$args = array(
'post_type' => $data['post_type'],
'post_status' => 'publish',
'title' => $data['post_title'],
'posts_per_page' => 1,
);
$existing_posts = get_posts($args);
// If a post with the same title was found, set the post status to draft and add a notice.
if (!empty($existing_posts)) {
$data['post_status'] = 'draft';
add_post_meta($postarr['ID'], '_unique_post_title_checker_notice', 'Post not published because a post with the same title already exists.');
}
return $data;
}
add_filter(‘wp_insert_post_data’, ‘unique_post_title_checker’, 10, 2);