WordPress.org

Forums

[resolved] wp_insert_post() only creates 'draft' no 'publish' posts (3 posts)

  1. greatdealsonnet
    Member
    Posted 7 years ago #

    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 ?

  2. fitztrev
    Member
    Posted 7 years ago #

    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.

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic