0

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?

1

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.

0

Insert Data in Custom Post From Front End

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "product") {

    $title     = $_POST['title'];
    $post_type = 'product';
    //the array of arguements to be inserted with wp_insert_post
    $front_post = array(
    'post_title'    => $title,
    'post_status'   => 'publish',          
    'post_type'     => $post_type 
    );

    //insert the the post into database by passing $new_post to wp_insert_post
    //store our post ID in a variable $pid
    $post_id = wp_insert_post($front_post);
    //we now use $pid (post id) to help add out post meta data
    update_post_meta($post_id, "short_description", @$_POST["short_description"]);
    update_post_meta($post_id, "price", @$_POST["price"]);
    update_post_meta($post_id, "length", @$_POST["length"]);

HTML Code Here

<form method="POST">
<label>Product Name</label>
        <input type="text" value="" class="input-xlarge" name='title'>
        <label>Product Description</label>
        <textarea value="" rows="3" class="input-xlarge" name='short_description'>
        </textarea>
 <label>Price</label>
        <input type="text" value="" class="input-xlarge" name='price'>
        <label>Dimensions (in):</label>
        <input type="text" value="" class="input-xlarge" name='length' placeholder="Length">
  <div>
            <button class="btn btn-primary">Add Product</button>
        </div>
        <input type="hidden" name="action" value="product" />
 </from>
-1

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
  • 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

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.