I am working on a wordpress site where users can submit there own posts. I would like to prevent users from adding a post with the same post_title
This is how i currently check to see if the post title exists:
$posts = $wpdb->get_col("SELECT post_name FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' OR post_status = 'trash' OR post_status = 'draft'");
gets the post title from the form
$alias = $_POST['post_title'];
checks if post_title is empty if it is create a random post_title else use the user defined one
if ($_POST['post_title'] == ''){
$alias = generateRandomString();
} else {
$alias = $_POST['post_title'];
}
I then check to see if the post_title already exists like this:
if (in_array($alias, $items)){
echo '<p style="color:#fff">Sorry, That Title already exists!</p>';
} else {
//insert the post
}
My issue is that wordpress will add a number after the post title if it does already exist. Is there a way to prevent wordpress from doing this?
The output of the above above shows this:
Array ( [0] => db [1] => abi [2] => ybd [3] => cc [4] => cc-2 [5] => fb )
I need to prevent the cc-2 from happening.