WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to add a page to WordPress programatically. I am using this code:

 function add_media_page(){
if(!(is_page('My New Post'))){

// Create post object
  $my_post = array(
     'post_title' => 'My new Post',
     'post_content' => 'This is my post.',
     'post_status' => 'publish',
     'post_author' => 1,
     'post_type' => ['page']

// Insert the post into the database
  wp_insert_post( $my_post );
add_action( 'init', 'add_media_page' ); 

However when i use the last line in the array 'post_type' => ['page'] it causes the admin to crash and show a blank page. Does anyone know what's happening here

share|improve this question
why are there square-brackets around it? You aren't referring to an object or an array, but passing a string as a value within the $my_postarray - so it should be just 'page'. – OleVik Jan 13 '12 at 21:19
it's totally unhelpful when people downmark questions without bothering to mention why - it's supposed to be used if someone shows "no research effort" or if it's an "unclear or unuseful post"; not for if someone makes an error in the code, which I can only assume is why it's been downvoted. Otherwise why bother with a Q&A site in the first place since we're all so clever – byronyasgur Jan 14 '12 at 1:13
Sorry, but I haven't downmarked it - that must've been someone else. I just added my comment as a quick partial answer, because I wasn't sure if my code-correction would be a complete answer. It is in my view a valid question - as I've had use of a similar method before - so I voted it back up to 0. – OleVik Jan 14 '12 at 2:50
thanks - no i didnt necessarily think you did - but thanks for pointing out my error; that was it as it so happens. – byronyasgur Jan 14 '12 at 15:17

'post_type' => ['page'] must be 'post_type' => 'page'. Just look at the example here: http://codex.wordpress.org/Function_Reference/wp_insert_post#Example

share|improve this answer

Try this within your code in functions.php

global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
$post_id = wp_insert_post($new_post);
share|improve this answer
up vote -1 down vote accepted

'post_type' => ['page'] is wrong; it should be 'post_type' => 'page'

share|improve this answer
why the downvote? – byronyasgur Feb 14 '13 at 21:45
I think because your answer was the same as mine, but 4 days later. Now, a year after this, you unaccept my answer and accept your answer. Bit strange, don't you think? – Rob Vermeer Feb 15 '13 at 13:04
I only did that because I don't believe that's what downvotes are for. The tick is MY choice, not yours! – byronyasgur Feb 15 '13 at 13:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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