1

我正在使用以下代码插入自定义帖子类型:

if(isset($_POST['submit'])){

    $post_id = wp_insert_post( array(
                    'post_status' => 'publish',
                    'post_type' => 'custom_type',
                    'post_title' => 'Some post',
                    'post_content' => 'Lorem ipsum'
                ) );

}

我收到错误页面;标题:“WordPress 失败通知”和页面中的文字“您确定要这样做吗?

请再试一次。”

但是当我尝试插入 post_type: post 时,一切正常。当我尝试在没有 isset 的情况下插入时,一切正常;

$post_id = wp_insert_post( array(
                        'post_status' => 'publish',
                        'post_type' => 'custom_type',
                        'post_title' => 'Some post',
                        'post_content' => 'Lorem ipsum'
                    ) );

有人有同样的问题吗?

4

4 回答 4

3

我没有找到除此之外的解决方案(并且它有效):

if(isset($_POST['submit'])){

    $post_id = wp_insert_post( array(
                    'post_status' => 'publish',
                    'post_type' => 'post',
                    'post_title' => 'Some post',
                    'post_content' => 'Lorem ipsum'
                ) );

$post_type = 'custom_type';

$query = "UPDATE {$wpdb->prefix}posts SET post_type='".$post_type."' WHERE id='".$post_id."' LIMIT 1";

GLOBAL $wpdb; 

$wpdb->query($query);

}
于 2012-08-20T15:13:16.033 回答
1

插入新的自定义帖子类型时遇到了同样的问题。通过在调用 wp_insert_post 函数之前取消设置 $_POST 找到了临时解决方案。

于 2012-08-25T23:33:44.857 回答
1

我让我的工作:

PHP,放在标题之前:

if(isset($_POST['new_post']) == '1') {

$new_post = array(
      'ID' => '',
      'post_type' => 'customtype', // Custom Post Type Slug
      'post_status' => 'publish',
      'post_title' => $_POST['post_title'],
    );

$post_id = wp_insert_post($new_post);
$post = get_post($post_id);
}

在 HTML 中:

<form method="post" action="">

      <input name="post_title" type="text" />

      <input type="hidden" name="new_post" value="1" />
      <input type="submit" name="submit" value="Post" />

</form>
于 2013-06-24T22:58:59.703 回答
0

试试这个,这是表单标题

<form id="insert_new_lesson" name="insert_new_lesson" method="post" action="" class="lesson-form">;

我做的if语句是这样的

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && 'insert_new_lesson' == $_POST['action'] )
于 2013-02-14T10:54:57.687 回答