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 ); endif;
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="" /> </form>
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?