0

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?

5

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());
  • thank you for this. I did initially have those date fields excluded, but for some reason the gmt date options were being set to 0000-00...etc. I added the fields manually to resolve the issue – Dan Hastings Apr 2 '17 at 16:56
  • Try that again, with post_status set to publish, but without date fields. Maybe it would work this time? – Greeso Apr 2 '17 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.