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.

  • Did you try this function with user sessions.?. Sometimes it requires strong user sessions to submit a post. – Kvvaradha Dec 26 '15 at 1:31

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.

| 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. – oschloebe Feb 15 '14 at 14:03
  • @oschloebe how did you fix this? – Saeesh Tendulkar Sep 9 at 8:19

Instead of wp_insert_posts, create your own "call" using $wpdb which you can then set your own database e.g.

$table_name = $wpdb->prefix . 'tablename';
    // data to add

You can read more on custom tables at http://codex.wordpress.org/Creating_Tables_with_Plugins

| improve this answer | |

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.