wp_insert_post works, but the post isn’t visible in the admin post list or front end

I am trying to create a page on a site during plugin activation. The code is working without error and the function call returns a post ID, but when i check the list of pages in the admin area there is nothing there. WordPress is detecting them as if i go to the pages menu item, the number uinder All() has increased, but the number of published pages has not.

$postarr["post_content"] = "[submission_widget]";
$postarr["post_title"] = "Submit Content";
$postarr["post_status"] = "published";
$postarr["post_type"] = "page";
$postarr["post_name"] = sanitize_title("Submit Content");
$postarr["post_date_gmt"] = date('Y-m-d H:i:s', time());
$postarr["post_date"] = date('Y-m-d H:i:s', time());
$postarr["post_modified"] = date('Y-m-d H:i:s', time());
$postarr["post_modified_gmt"] = date('Y-m-d H:i:s', time());
$postid = wp_insert_post($postarr, true);
update_post_meta( $postid, '_visibility', 'visible' );

To double check, i went into the posts table in the DB and i can see the post is there. I dont see anything wrong with it, but something is obviously missing. Where am i going wrong?

1 Answer
1

The problem is $postarr["post_status"] = "published"; you should change it to

$postarr["post_status"] = "publish"; // Set it to 'publish', not 'published'

Now, this is a bit off topic, but you may want to omit the following (those are created by the wp_insert_post() by default):

$postarr["post_date_gmt"] = date('Y-m-d H:i:s', time());
$postarr["post_date"] = date('Y-m-d H:i:s', time());
$postarr["post_modified"] = date('Y-m-d H:i:s', time());
$postarr["post_modified_gmt"] = date('Y-m-d H:i:s', time());

Leave a Comment