I can't get this to work. I have a page where users can submit their posts. There, ideally I want to show all the categories and let them choose via a dropdown the category to assign the post. I couldn't manage this as I am a newbie regarding PHP so I decided to go step by step and first start with adding an empty input field where users can just add a random text that will become the category of that post.

To do so, I have done the following:

                   if(isset($_POST['entry']) AND !$_POST['entry'] == ""):
                        $my_post = array();
                        $my_post['post_title'] = $_POST['title'];
                        $my_post['post_content'] = $_POST['entry'];
                        $my_post['post_status'] = 'publish';

                        $cat_name = sanitize_text_field($_POST['newcat']);
                        $my_post ['cat_name'] = $cat_name; 

                        $my_post['post_author'] = get_current_user_id();;

                        // add post to database
                        $id = wp_insert_post( $my_post );

So, as you can see, I am trying to add the category by linking it to $my_post and the "newcat" input field.

And the form:

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

<label for="newcat">Category Name</label>
<input type="text" name="newcat" value="" />


Post title and content are working (removed it from the code above), but category isn't working at all.

Can someone help me and explain what I am doing wrong?


Please try to use this function https://codex.wordpress.org/Function_Reference/wp_dropdown_categories

wp_dropdown_categories() - Display the HTML dropdown list of categories

    <form id="category-select" class="category-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
        <?php wp_dropdown_categories(); ?>
        <input type="submit" name="submit" value="view" />
  • Yes, but that's just for showing. How do I make sure it's submitted with the post and becomes the category of that post? – Siyah May 2 '19 at 21:17
  • To set a category for the post you need to use this function codex.wordpress.org/Function_Reference/wp_set_post_categories wp_set_post_categories( $id, $_POST['category']); You need to set this code below the line $id = wp_insert_post( $my_post ); – Oleh May 2 '19 at 21:19
  • I don't get it. How do you want me to insert it into the post, @oleh? – Siyah May 2 '19 at 21:39
  • If you want to create category dynamically you need to use this functions wp_insert_category - codex.wordpress.org/Function_Reference/wp_insert_category Then you need to assign category ID to your post. Another way(my solution) is to allow users to use already created categories( you can show this field in the form by this function wp_dropdown_categories()). – Oleh May 2 '19 at 21:46
  • This doesn't do at all what I want and doesn't answer the question. – Siyah May 3 '19 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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