I have a solution for the fact that users can now add categories to their posts. Problem: they don't know which already exist and which do. Therefore, I'd like to go to a route where users can choose (checkbox?) the categories that exist.

My question: how to do this properly?

My code is as follows:

                           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;

$category_id = get_cat_ID( $_POST['newcat'] );

if ( ! $category_id ) {
    if ( ! is_admin() ) {
$args = array(
    'description' => "Category description",
    'parent' => 0);
    $category_id = wp_insert_term( $_POST['newcat'], "category", $args );

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

$my_post['tax_input'] = array('category' => $category_id);

wp_insert_post( $my_post );

And then I am showing the dropdown categories, yet I can't save my choice when adding the checkboxes for categories.

$categories=get_categories();   foreach($categories as $category) {     echo "<input type='checkbox' name='mychecky' value='$category->term_id' />";    echo $category->cat_name;
    echo '<br>';    }

How can I save the chosen category per checklist for my post?

Your Answer

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

Browse other questions tagged or ask your own question.