wp_insert_post does not insert string with apostrophes correctly


I’m using wp_insert_post (for several reasons) in a script I’m writing and it’s causing any string that has an apostrophe in it to be truncated up to that character (including the character). Here’s the code I’ve got to test it:

$question = "If you’re in need, this is who you call";

echo $question."<br />";

$post = array(
    'post_title'     => $question,
    'post_status'    => 'publish',
    'post_type'      => 'question'

echo $post['post_title'];

$the_post_id = wp_insert_post($post);

It’s weird because when I echo out both the strings, the echo the correct thing on both ends so I’ve isolated it down to the wp_insert_post function that’s causing the issue. When I view it in WordPress, the title is just ‘If you’ and then ends.

I found a similar problem here:


But there doesn’t seem to be any solution to it on that page. Any ideas on what’s going on here and how to solve it? No idea why it’s causing it. I’m looking into it to see if has something to do with something else in my code but it seems like it shouldn’t because the array element seems formatting just fine when it’s echoed out before the wp_insert_post() function.

Thanks for your help!


I should have specified that these are having to be called from an array where I’m grabbing data from a different source so I won’t necessarily have access to the strings directly. I tried mysql_real_escape_string() which should do that job automatically as well and that didn’t seem to work…

MxmastaMills 9 years 2014-06-03T00:10:16-05:00 0 Answers 70 views 0

Leave an answer