Hi, I wanted to create a function like that.
It should be very easy, but in my case, I have to deal with a lot (>2000) posts. For some reason, get_posts() doesn’t work, because the data amount created by this is too big.
I had to find a way to query just the titles, not all the other data which is not needed, eg the content, the custom fields, etc.
This is what I’ve figured out and works for me:
function my_post_exists($suchtitel){
$suchtitel=strtoupper($suchtitel);
$suchtitel = trim($suchtitel, ' ');
global $wpdb;
$meinetitel = $wpdb->get_col("SELECT post_title
FROM $wpdb->posts
WHERE post_status = 'publish'
ORDER BY post_title");
foreach($meinetitel as $meintitel){
$meintitel=strtoupper($meintitel);
if($suchtitel==$meintitel){
return true;
}
}
}
You could place this function in your “functions.php” and call it in your template where you need it.
The function is not case sensitive and cuts spaces from the end and the beginning of the search-title.
If someone could tell me if the query is safe for sql-injections, please tell me, because i’m not very experienced in that.