【问题标题】:wp_insert_post not adding categorywp_insert_post 未添加类别
【发布时间】:2013-07-06 12:53:15
【问题描述】:

我正在构建一个 WordPress 主题,人们可以在其中使用 wp_insert_post 提交帖子。下面的代码添加了帖子标题,但没有添加用户指定的类别。相反,它将它放在uncategorized 中。

如何在提交时将新类别添加到帖子中?

if(isset($_POST['new_post']) == '1') {
    $post_title = $_POST['post_title'];
    $new_cat_ID = $_POST['category'];

    //Checking if category already there
    $cat_ID = get_cat_ID( $_POST['newcat'] );

    //If not create new category
    if($cat_ID == 0) {
       $cat_name = array('cat_name' => $_POST['newcat']);
       wp_insert_category($cat_name);
    }

    //Get ID of newly created category
    $new_cat_ID = get_cat_ID($_POST['newcat']);

    // Create post object
    $new_post = array(
        'ID' => '',
        'post_title' => $post_title,
        'post_status' => 'publish',
        'post_author' => $user->ID,
        'tax_input' => array( 'category' => $new_cat_ID )
    );

    // Insert the post into the database
    $post_id = wp_insert_post($new_post);

    // This will redirect you to the newly created post
    $post = get_post($post_id);
    wp_redirect( home_url() ); 
    exit;
}

这是表单的 HTML:

<form style="" action="" method="post" id="foo">
<input type="hidden" name="new_post" value="1"/>
<input type="text" name="post_title" value="title" id="input-title"/>
<input type="text" name="category" value="apples" id="category" />
<input type="submit" value="Login">
</form>

【问题讨论】:

  • 您的input namecategory,但您正在检查$_POST['newcat']。阅读Data Validation 以确保您的代码安全。您还需要this
  • 谢谢!我了解类别部分,但我仍然不确定将 wp_insert_term 放在哪里。你介意修改我的代码给我看吗?

标签: wordpress forms post insert frontend


【解决方案1】:

category 输入应该是:

<input type="text" name="newcategory" value="apples" id="category" />

在我的测试中,wp_insert_category 不起作用,必须使用wp_insert_term(根据this forum thread)。

您的wp_redirect 没有按照您的意愿行事。 //This will redirect you to the newly created post 部分完全错误。

以下是一个使用wp_nonce_field 添加安全层的工作示例,但您必须添加 User Input Data Validation
另外,我在登录时进行测试,所以它可以工作。您的代码无法处理 $user-&gt;ID,请研究 get_current_user 以解决此问题。

<?php
if ( isset( $_POST['noncename_so_17539370'] ) && wp_verify_nonce( $_POST['noncename_so_17539370'], 'nonce_so_17539370' ) )
{
    if( isset($_POST['new_post']) == '1' ) {

        //Checking if category already there
        $cat_ID = get_cat_ID( $_POST['newcat'] );

        //If not create new category
        if( !$cat_ID ) {
            $arg = array( 'description' => "my description", 'parent' => 0 );
            $cat_ID = wp_insert_term($_POST['newcat'], "category", $arg);
        }

        // Create post object
        $new_post = array(
            'ID' => '',
            'post_title' => $_POST['post_title'],
            'post_status' => 'publish',
            //'post_author' => $user->ID,
            'tax_input' => array( 'category' => $cat_ID )
        );

        // Insert the post into the database
        $post_id = wp_insert_post($new_post);

        // This will redirect you to the newly created post
        $post = ;
        wp_redirect( get_permalink( $post_id ) ); 
        exit;
    }
} else {
    echo 'ERROR';
}
?>
    <form style="" action="" method="post" id="foo">
    <?php wp_nonce_field( 'nonce_so_17539370', 'noncename_so_17539370' ); ?>
    <input type="hidden" name="new_post" value="1"/>
    <input type="text" name="post_title" value="title" id="input-title"/>
    <input type="text" name="newcat" value="apples" id="category" />
    <input type="submit" value="Login">
    </form>

【讨论】:

  • 是的!你太棒了。这解决了我的问题。我正在为此撕毁我的头发。谢谢!
  • 再次考虑这段代码,我想知道您是否可以帮我制作它,以便未登录到 WordPress 的用户可以添加类别。可能将这些帖子归于一个作者。这甚至可能吗?如果可以,怎么办?谢谢!
  • @ewcost,这里的规则与论坛不同。你必须打开一个新问题。 About页面有简要介绍。在询问新问题之前,请在此处和 WordPress Development 进行研究。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多