wp_insert_post没有添加类别

时间:2013-07-09 03:25:40

标签: wordpress forms post insert frontend

我正在构建一个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>

1 个答案:

答案 0 :(得分: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->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>