Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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'");


if(!empty($mypostids)){
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.

https://dl.dropbox.com/u/3132388/screenshot_415.jpg

share|improve this question
add comment

1 Answer

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
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 '12 at 21:05
add comment

Your Answer

 
discard

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.