Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a form in a page template that allows users to submit posts into a custom post type from the front-end:

$post = array(
    'post_status' => 'draft',
    'post_type' => 'stories',
    'post_content' => wp_kses_post( $story_content ),
    'post_title' => esc_attr( wp_kses( $story_title, array() ) ),
    'post_author' => 4,
);

// Insert story into db
$post_success = wp_insert_post( $post );

It works, but I receive these notices

PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/comment-template.php on line 776
PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/comment-template.php on line 793
PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/general-template.php on line 1645
PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/link-template.php on line 1106
PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/link-template.php on line 1148
PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/link-template.php on line 1106
PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/link-template.php on line 1148

I've looked at the files referenced, and it seems to be related to the comment_status and ping_status of the submitted post, however, I assume that the defaults for wp_insert_post should input values for those. Even when I set them in my args array, I get the notices. How to fix this to get rid of the notices?

share|improve this question

2 Answers 2

Still can't comment it seems, but you have an extra comma in your array.

 $post = array(
     'post_status' => 'draft',
     'post_type' => 'stories',
     'post_content' => wp_kses_post( $story_content ),
     'post_title' => esc_attr( wp_kses( $story_title, array() ) ),
     'post_author' => 4, //EXTRA COMMA HERE
 );

Maybe that's your problem.

 'post_author' => 4,

should be

 'post_author' => 4
share|improve this answer
    
Thanks, however the extra comma isn't the issue. That is valid in a PHP array. I've figured it out, and will post back after I've given others a chance to answer. –  jjeaton Jan 17 '12 at 22:09
up vote 1 down vote accepted

The issue is in using the $post variable for arguments. After changing this to something like $new_post I no longer get the notices.

There must be some conflict with the global $post that WordPress uses.

I took this from the Codex, but looking back at the page, I realize that $post was just referring the argument name and the actual example in the codex does not use $post for the args array.

share|improve this answer

Your Answer

 
discard

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.