WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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

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
    
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
up vote 1 down vote accepted

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 '14 at 14:03

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';
$wpdb->insert( 
 $table_name, 
 array(
    // data to add
 ) 
);

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

share|improve this answer

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.