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.

For whatever reason wp_insert_post doesn't work for a custom post type.

Here's the code snippet I'm using.

$new_post = array(
    'post_type' => 'rsp_synonym',
    'post_title' => wp_strip_all_tags( $row['word'] ),
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => 2,
    'post_content' => '',
    'post_status' => 'publish',
    'comment_status' => 'open',
    'ping_status' => 'open'
);

$post_id = wp_insert_post( $new_post );

if (is_wp_error($post_id)) {
    $errors = $post_id->get_error_messages();
    foreach ($errors as $error) {
        echo "- " . $error . "<br />";
    }
}

Strange enough, it doesn't even return a WP_Error using the if (is_wp_error($post_id)) conditional. But still, no posts are being added.

The custom post type named as rsp_synonym has been registered before using this call.

share|improve this question

1 Answer 1

Try add the second parameter like:

$post_id = wp_insert_post( $new_post, true );

If it still won't work, please var_dump the $post_id and paste here.

share|improve this answer
    
Hi, thanks! That did help a lot. As I'm using mysql_connect and mysql_select_db in my script in order to get data from a DB different than the one WP uses, it seems that wp_insert_post changes its DB connection too. Weird. –  Alphawolf Feb 15 at 14:03

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.