WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


share|improve this question

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

share|improve this answer
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 '12 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 – Mridul Aggarwal Oct 21 '12 at 18:21
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 '12 at 21:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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