Support » WP-Advanced » [Resolved] wp_insert_post() only creates ‘draft’ no ‘publish’ posts

[Resolved] wp_insert_post() only creates ‘draft’ no ‘publish’ posts

  • The following function works well for creating Drafts but when using $status=’publish’ I get an errror:

    Warning: in_array() [function.in-array]: Wrong datatype for second argument in …wp-includes\post.php on line 703
    Fatal error: Call to undefined function: get_userdata() in …\wp-includes\link-template.php on line 85

    function wp_ai_post($title, $content, $timestamp = null, $category = null, $status = 'draft', $authorid = null, $allowpings = true, $allowcomments = true ) {
    global $wpdb;
    $date = ($timestamp) ? gmdate('Y-m-d H:i:s', $timestamp) : null;
       $postid = wp_insert_post(array(
    		'post_title' 	            => $title,
      		'post_content'  	        => $content,
      		'post_content_filtered'  	=> $content,
      		'post_category'           => $category,
      		'post_status' 	          => $status,
      		'post_author'             => $authorid,
      		'post_date'               => $date,
      		'comment_status'          => $allowcomments,
      		'ping_status'             => $allowpings
    do_action('publish_post', $postid);
    return $postid;

    Any ideas ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Not sure about the error, but you can pass wp_insert_post ‘post_status’ = ‘publish’ to get it to publish the post. It defaults to draft so you have to specify the status yourself.

    Moderator Samuel Wood (Otto)


    WordPress.org Tech Dude

    You’re getting the warning because your post_name is not unique. The post_name is generated from the post_title unless you specify it manually, and yours probably conflicts with another one.

    You’re getting the fatal error on get_userdata because you are not including all the necessary files or something. You need to include wp-blog-header.php to load WordPress correctly.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] wp_insert_post() only creates ‘draft’ no ‘publish’ posts’ is closed to new replies.