How can a category be assigned to a new front-end post submission?

Posting works, Category is not assigned. Would like to be able to add more than one, if possible.
Both are CPT and custom taxonomies.

if ( isset( $_POST['submitted'] ) && isset( $_POST['new_post_nonce_field'] ) && wp_verify_nonce( $_POST['new_post_nonce_field'], 'new_post_nonce_action' ) ) {

    $new_post = array(
        'post_content'  =>  $_POST['post-content'],
        'post_title'    =>  wp_strip_all_tags( $_POST['post-title'] ),
        'post_type'     => 'custom_pt',
        'post_category' =>  array( 
            'custom_tax' => $_POST['custom_tax']

    if ( !$hasError == true ) {            
        $post_id = wp_insert_post( $new_post );        
        if ( post_id ) {
            wp_redirect( home_url() );


    <form id="new_post" method="POST" action="" enctype="multipart/form-data">

        <label for="post-title">The Title</label>
        <input id="post-title" name="post-title" type="text" />

        <label for="post-content">The Content</label>
        <textarea id="post-content" name="post-content"></textarea>

        <label for="custom_tax">The Categories</label>
        <?php wp_dropdown_categories( 'tab_index=10&taxonomy=custom_tax&name=custom_tax&class=custom_tax&show_option_all=Select a category' ); ?>

    <?php wp_nonce_field( 'new_post_nonce_action', 'new_post_nonce_field' ); ?>

        <button type="submit">Publish</button>
        <input type="hidden" name="submitted" id="submitted" value="true" />


Thank You.

3 Answers 3


I checked and it works with this:

$new_post = array(
    'post_content'  =>  $_POST['post-content'],
    'post_title'    =>  wp_strip_all_tags( $_POST['post-title'] ),
    'post_type'     => 'custom_pt'

note: I used a text input for the single category

<input id="input-name-value" name="input-name-value" type="text" />

I havent tried it with a select - option dropdown. or more than 1 category.

$post_id = wp_insert_post( $new_post );

wp_set_object_terms( $post_id, $_POST['input-name-value'], 'yourCategory' );

It would be nice if someone could pimp this out in order to get full functionallity.



Found this:

$postCats = trim( $_POST['post-cats']; )
$postCats = explode( ',', $postCats );

wp_set_object_terms( $post_id, array( $postCats[0], $postCats[1], $postCats[2], $postCats[3], $postCats[4] ), 'category' );

Works the same for tags

I'm still wondering if there is a way to add unlimited categories without declaring a limited number in an array (like above).

'd be great if someone helped me out with that.


By using wp_set_object_terms you can set post category , for more details please follow this link https://codex.wordpress.org/Function_Reference/wp_set_object_terms


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.