• Okay, so I have a form that submits a post from the front-end
    the code for submitting looks like this:

    $post = array(
    	'post_author'   => $userdata->ID,
    	'post_title'	=> $title,
    	'post_category'	=> $category,
    	'tags_input'	=> $tags,
    	'post_status'	=> 'publish',
    	'post_date' 	=> date('Y-m-d H:i:s'),
    	'post_date_gmt' => date('Y-m-d H:i:s'),
    	'post_type'	=> 'post'
    );
    $post_id = wp_insert_post($post, false);
    if($post_id != 0)
    {
    	wp_redirect(get_bloginfo('url')."/?p=$post_id");
    
    }else{
    	die('error');
    }

    Problem is that some times it is returning an ID and redirecting to the post only that the post was never actually created. For instance if a post is submitted twice.

    I checked the codex and it says that it should only return the post ID if it was inserted into the database?

    And before anyone ask the only plugins I have are Akismet and WP-Optimize

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter slaaxy

    (@slaaxy)

    If anyone have any idea on how to debug this, that would be great too.

    To debug, replace $post_id = wp_insert_post($post, false); with $post_id = wp_insert_post($post, true);

    Also, add 'ID' => '' in your array definition if you are inserting or the appropiate ID; just in case.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_insert_post not working’ is closed to new replies.