This is very perplexing and hope someone can help me. Here is some code I'm using to see if a title exists in wp_posts table

$mypostids = $wpdb->get_results("select ID from $wpdb->posts where   post_title='$thisTitle'");

echo $thisTitle.' already exists'; }else{
    echo $thisTitle.' does not exist'; 

where $thisTitle is simply the title. Here is the output from this

Pics: Sarah Michelle Gellar & Freddie Prinze Jr step out post-baby already exists

Saturday Night Live lampoons Pandora music channels, but don't expect NBC to stream it online does not exist

Report: Adele is a mum! Baby Konecki born Friday already exists

As you can see, the output says the title "Saturday Night Live, etc." doesn't exist, but you can see by the image (in the link below) it certainly does exist.

I have no idea why this is happening. Am I missing something obvious? Thanks.


1 Answer 1


It's most likely due to the title not being matched exactly. You might be having an extra space before/after/between the title either here or in the database

  • right..well, the way it works now in my code is that if it doesn't exist in the database, I put it there using wp_insert_post, so it shouldn't have extra spaces, but I'll try to trim and see if that helps
    – Allen
    Oct 21, 2012 at 18:11
  • i don't think you should depend on that. wp_insert_post itself has some hooks & sanitization code in it, which could change the title Oct 21, 2012 at 18:21
  • 1
    right...well, I changed the way everything works now, by using the wp_post_meta table and relying on the permalink, and this seemed to solve the problem (at least the permalink doesn't have spaces, smart quotes, etc that makes search by title so difficult)
    – Allen
    Oct 21, 2012 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.