Support » Fixing WordPress » Multple Categories Checkbox

  • Hi guys,

    I need some help with the following script:

    <strong><?php echo __('Business Sector:*  ', 'ghh-submit-listing-plg') ?></strong><br>
                <?php
                    $categories=get_categories();
                    foreach( $categories as $category ) {
                        echo "<input type='checkbox' name='category' value='$category->term_id' />";
                        echo $category->cat_name;
                        echo '<br>';
                    }
                ?>
                <span class="error-message"><?php echo $errCat; ?></span><br>
            </div>

    I basically am displaying checkboxes and I want to be able to store all selected categories meta to the database and assign them to the post. This is the front-end: http://prntscr.com/a7sn5v

    And this is how I am currently saving the meta, which is obviously wrong:

    // Post Properties
    $new_post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_category' => array($_POST['cat']),  // Usable for custom taxonomies too
        'tags_input'    => $tags,
        'post_status'   => 'pending',           // Choose: publish, preview, future, draft, etc.
        'post_type'     => 'post',  //'post',page' or use a custom post type if you want to
        'post_author'   => $authorid //Author ID
    );
    
    //save the new post
    $pid = wp_insert_post($new_post);

    Please shed some light on how I should go about saving multiple selected categories to the post. Any point in the right direction will be highly appreciated.

  • The topic ‘Multple Categories Checkbox’ is closed to new replies.