Afaf
Afaf

Reputation: 654

Wordpress wp_insert_post doesn't work

Here is my script, to let you know I use it inside my test file

$my_post = array(
                'post_title' => "post test",
                'post_date' => current_time('mysql'),
                'post_content' => 'This is my post.',
                'post_status' => 'publish', 
                'post_author'   => 1,
                'post_category' => array(1)

            ); 
        $post_id= wp_insert_post($my_post);
        var_dump($post_id);

Upvotes: 1

Views: 1592

Answers (3)

devLad
devLad

Reputation: 16

Try to remove status param or change it to another status but not publish

Upvotes: 0

Ratna Raju
Ratna Raju

Reputation: 37

  1. First check the query by using following line of code under wp_insert_post call:

    exit(var_dump( $wpdb->last_query));

    the query will be displaying if you run the code... try to execute the same code in phpmyadmin sql panel or any other db tool.. you will get to know the error.

    1. else change the above mentioned line of code like exit(var_dump( $wpdb->last_query)); try to execute you will get to know the error.

Upvotes: 1

Mohsin khan
Mohsin khan

Reputation: 136

I think your date format is incorrect so please use this code

$my_post = array(
                'post_title' => "post test",
                'post_date' => date('y-m-d H:i:s'),
                'post_content' => 'This is my post.',
                'post_status' => 'publish', 
                'post_author'   => 1,
                'post_category' => array(1)

            ); 
        $post_id= wp_insert_post($my_post);
        var_dump($post_id);

Upvotes: 1

Related Questions