前端提交自定义帖子类型时的注意事项

时间:2012-01-17 作者:jjeaton

我在页面模板中有一个表单,允许用户从前端将帖子提交到自定义帖子类型:

$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 );
这很有效,但我收到了这些通知

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
我查看了引用的文件,它似乎与comment_statusping_status 但是,我假设wp_insert_post 应该为这些输入值。即使我在args数组中设置它们,也会收到通知。如何解决此问题以摆脱通知?

3 个回复
最合适的回答,由SO网友:jjeaton 整理而成

问题在于使用$post 参数的变量。将此更改为以下内容后$new_post 我不再收到通知。

一定与global $post WordPress使用的。

我从抄本上拿了这个,但回头看,我意识到$post 只是引用了参数名称,而codex中的实际示例没有使用$post 对于args数组。

SO网友:Vivek Tamrakar

从前端在自定义帖子中插入数据

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代码

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

SO网友:Shane

似乎仍然无法对其进行注释,但数组中有一个额外的逗号。

 $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
 );
也许这是你的问题。

 \'post_author\' => 4,
应该是

 \'post_author\' => 4

结束

相关推荐

Displaying oEmbed errors?

有时,通过oEmbed嵌入项目是不可能的,例如,当YouTube视频已禁用嵌入时。The oEmbed service will return a 401 Unauthorized, 并且不会转换代码。有没有办法通知用户这一点?当前的工作流是非直观的(至少对我来说),我更喜欢在WordPress页面上,或者更好的是,在编辑器中显示一条消息,说明对象无法嵌入。