ewcost
ewcost

Reputation: 1

wp_insert_post not adding category

I'm building a WordPress theme where people can submit posts using wp_insert_post. The code below adds the post title but does not add the category specified by the user. Instead it puts it in uncategorized.

How do I get it to add the new category to the post when submitted?

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

Here's the HTML for the form:

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

Upvotes: 0

Views: 6826

Answers (1)

brasofilo
brasofilo

Reputation: 26065

The category input should be:

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

In my tests, wp_insert_category did not work and wp_insert_term had to be used (as per this forum thread).

Your wp_redirect is not taking where you thing it does. The section //This will redirect you to the newly created post is plain wrong.

The following is a working example with a security layer added with wp_nonce_field, but you must add User Input Data Validation.
Also, I'm testing while logged in, so it works. Your code does not take care of $user->ID, research for get_current_user to get this right.

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

Upvotes: 1

Related Questions