PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Selecting Multiple Categories

  1. Yes, I know... it's been asked 100 times already. :)

    I'm just wondering two things:
    1) why you can't use the conditional function on the Category drop down
    2) why can't you create a drop down of your own with the category ID in the "value" area and have that work?

    #1 I sort of understand... since categories are dynamic, or at least different per-site, you can't really have it create a list of "if then" functions.

    But that seems like #2 should work fine then because you would enter the categories in manually, just like any other drop down list.

    However, when I try that — entering in category names, and then the ID # in the "value" field — it doesn't work. When I look at the code generated it looks exactly the same as the code generated by the category drop down...

    Here's the custom drop down code generated...

    <div class="ginput_container">
    <select id="input_1_18" class="medium gfield_select" tabindex="3" name="input_18">
    <option value="53">Health</option>
    <option value="22">Food</option>
    <option value="54">Kids</option>
    </select>
    </div>

    And here's the code generated by the category drop down...

    <select id="input_1_7" class="medium gfield_select" tabindex="5" name="input_7">
    <option selected="selected" value="-1">Select a category</option>
    <option value="53">Health</option>
    <option value="22">Food</option>
    <option value="54">Kids</option>
    </select>

    ... but it's not assigning them to categories. So, obviously, there must be something in the JQuery, or PHP, or somewhere that causes the "submit" to submit those things differently.

    Seems like it should be easy — for "you" at least — to make it so that you can enter in category ID #s into the "value" area and have it submit them.

    Is it something you can help me with?

    The amount of categories and sub-categories that we have are way too many to have people just select from one drop down. But if we could get the conditional stuff working with them (either way... #1, or #2...) then that would work!

    Thanks!!!

    Posted 13 years ago on Thursday June 2, 2011 | Permalink
  2. Worked on this all night... still can't figure it out.

    Another possible option would be to not use the categories in WordPress, but set up custom meta fields for them. The problem I would have with this would be how to show the "category" pages and search by categories, etc.

    Hoping for some help today during normal business hours... but am trying to allow adequate time for a response to my inquiry. :D

    Posted 13 years ago on Thursday June 2, 2011 | Permalink
  3. Hi Parkeast,

    This code is not covered by support; however, it may be a good start for you in regards to selecting multiple categories. This would go in your theme's functions.php file.

    http://pastie.org/2009261

    To select multiple categories you will need to use a check box field. On the advanced tab you will see a checkbox to enable population by taxonomy. Then select the Categories taxonomy from the drop down.

    Good luck!

    Posted 13 years ago on Thursday June 2, 2011 | Permalink
  4. Thanks!
    I will give it a try and let you know. :D

    Posted 13 years ago on Thursday June 2, 2011 | Permalink
  5. Okay... this is so close to working how I need it to, but it's just not doing it.

    I added the code to my functions.php and edited line 111 so added the following to its parameters...

    $terms = get_terms($taxonomy, 'orderby=name&order=ASC&hide_empty=0&hierarchical=1&include=22,52,53,54,117');

    That worked great, and showed just my primary categories when I used the taxonomy in the check list and chose "categories."

    However... when I then tried to set the conditional fields to show or not show based on that choices from that checklist it wouldn't work.

    Also, I can't make more "category" fields show based on this.

    What is working great — as far as appearing as I want it to and giving the user the choices I want to give them — is simply using the check boxes as is and entering in my categories manually.

    ALL that needs to happen really is to have the ability to have the "value" field be the category ID # and post in the corresponding category.

    Is this just completely out of the question? It seems like it should be so easy to do... but, alas, I am lost!

    Thanks for the help so far... I will keep you updated, and please (anyone) let me know if you know a way to do this!

    Posted 13 years ago on Thursday June 2, 2011 | Permalink
  6. (sorry, the code above isn't posting right...)

    Posted 13 years ago on Thursday June 2, 2011 | Permalink
  7. So the thing that appears to make WP post into a category via a check list is the code...

    name="post_category[]"

    On line 164 of the common.php file the name for the field is defined as...

    name='input_%s'

    If you change this, you break it... if you add the "post_category[]" in addition to what's there already it doesn't do anything.

    I'm determined to figure this out... there are SO many people I've read who want it!

    Posted 13 years ago on Thursday June 2, 2011 | Permalink
  8. 15 minutes till end of day... still no luck from my side... I'll be working long past 15 more minutes though. ;)

    Posted 13 years ago on Thursday June 2, 2011 | Permalink
  9. Another option, that might be even easier, is to use the categories code in your functions.php file, but alter it so it populates the "Field Choices" with the categories you select as well. Then the back-end form could use it with conditional logic, and the front end with posting to categories.

    Posted 13 years ago on Thursday June 2, 2011 | Permalink
  10. Okay... here's where I am at 11:00 p.m. EST...

    I have the common.php file open and have edited the line 1164 to be as follows...

    $choices.= sprintf("<li id='category-$choice_value' class='gchoice_$id'><input id='in-category-$choice_value' type='checkbox'  name='post_category[]' value='$choice_value' $logic_event $tabindex /><label for='choice_%s'>" . $choice["text"] . "</label></li>", $input_id, esc_attr($choice_value), $checked, $id, $disabled_text, $id, $choice["text"]);

    This now outputs like the input forms on the WP back-end where you can select multiple categories, but it's not posting in the categories...

    SECOND THING I tried... was to alter the categories.php file you linked to above so that it doesn't list all of the categories but only those that you type the ID # of in the values in the "Choices" area of the admin checkbox builder.

    So, I added this to the php file " $cat_value = $value;" and then changed line 107 to be as follows...

    function ounce_taxonomy_as_choices($taxonomy = "categories", $first_choice = '', $cat_value = '') {
        $terms = get_terms($taxonomy, 'orderby=name&hide_empty=0&hierarchical=1&include=' . $cat_value);

    But, it doesn't work either. Somewhere it's missing pulling in the $value.

    I'm a novice at PHP, so all of this is like asking a kindergartener to cook a meal with Chef Ramsay. It's fun until I've been at it for 36 hours.

    Hoping for some more help from someone.

    Posted 13 years ago on Friday June 3, 2011 | Permalink
  11. Just sent you an email. :)

    Posted 13 years ago on Friday June 3, 2011 | Permalink
  12. Just got an email from you. ;D

    Posted 13 years ago on Friday June 3, 2011 | Permalink
  13. hafizmohd
    Member

    Did it work from your end @parkeast?
    I try to post this, but maybe i didnt specify my exact problem.

    Posted 13 years ago on Saturday June 4, 2011 | Permalink
  14. Anonymous
    Unregistered

    What's the haps on this? I'd really like to be able to do this as well and I'm happy to edit my common.php to get it. Screenshots of the solution, please?? And why can't this be supported??

    Posted 13 years ago on Tuesday June 7, 2011 | Permalink
  15. Anonymous
    Unregistered

    Truth is that I would really love to just be able to have my categories show up in alphabetical order, but it looks like it's based on some obscure field. It's not based on date created. Can't we alphabetize the drop-down instead of laying out a bunch of checkboxes?

    Posted 13 years ago on Tuesday June 7, 2011 | Permalink
  16. Hi Joseph, we'd like to make the category field more functional but there are other fractures that have just been a higher priority thus far.

    Posted 13 years ago on Tuesday June 7, 2011 | Permalink
  17. Anonymous
    Unregistered

    Well, I can understand, but at the same time, my Global eBook Awards contest close at the end of the month and we're trying to ramp up, so this feature will be missed if it's not going to be ready right away.

    Posted 13 years ago on Thursday June 9, 2011 | Permalink
  18. Oh... hi all! Sorry, sleep ya know.
    Yes, it's working how I needed it to, thanks much to the help here!!

    Again, this isn't supported by them, but here's the "fix."

    Paste this in your theme's function.php file...

    /* Gravity Forms Categories */
    
    add_action('gform_post_submission', 'gform_add_categories', 10, 2);
    function gform_add_categories($entry, $form){
    
        if(!rgar($entry, 'post_id'))
            return;
    
        $post_id = rgar($entry, 'post_id');
        $valid_cat_ids = get_term_ids();
        $cat_ids = array();
    
        foreach($form['fields'] as $field){
    
            if(strpos($field['cssClass'], 'add_categories') === false)
                continue;
    
            foreach($entry as $key => $value){
    
                if(intval($key) != $field['id'] || !in_array($value, $valid_cat_ids))
                    continue;
    
                $cat_ids[] = (int) $value;
    
            }
    
        }
    
        wp_set_object_terms($post_id, $cat_ids, 'category', false);
    
    }
    
    function get_term_ids(){
    
        $categories = get_terms('category', 'hide_empty=0');
        $cat_ids = array();
    
        foreach($categories as $category)
            $cat_ids[] = $category->term_id;
    
        return $cat_ids;
    }
    
    function print_rr($array){
        echo '<pre>';
        print_r($array);
        echo '</pre>';
    }

    Then, when you add a check box, simply put the category ID # in the "value" field.
    You HAVE to put the IDs in for every category you want it to post into. So it is a "hack", but it works great.

    Posted 13 years ago on Thursday June 9, 2011 | Permalink
  19. Anonymous
    Unregistered

    @parkeast... THANK YOU

    This is just fantastic as I have about 80 categories and user-submitted posts.

    The only thing that could be improved is to allow two columns or three columns for formatting, but this is just great.

    Please see your work in action, parkeast!

    Posted 13 years ago on Saturday June 11, 2011 | Permalink
  20. parakmh
    Member

    Hello Gravity owners!
    Since we also faced the same problem, getting the idea from David Smith, we extended the functionality and created a plugin that you can not only use the normal post category control where you can select your desired categories with checkboxes but also use a Drop-Down menu, Multi Select or Radio Buttons for your category selection.

    As you can imagine, this plugin is distributed for free (GPL) and you can learn more and download in a relevant how to post on our site: Selecting Multiple Categories Addon for Gravity Forms

    We have extensively tested it and think that it's a very stable release. If you have ANY problems with it, please do tell us and we'll be happy to help.

    Hope it can help many with the same issue and thanks once again to David Smith for giving us something to work on ;)

    Posted 13 years ago on Thursday December 1, 2011 | Permalink
  21. I'm going to close this topic. If anyone has interest in the plugin solution coded by parakmh, please follow this topic:

    http://www.gravityhelp.com/forums/topic/selecting-multiple-categories-add-on-for-gravity-forms

    If anyone has specific questions about this functionality, not related to this 3rd party add-on, please feel free to open a new topic. Thank you.

    Posted 13 years ago on Friday December 2, 2011 | Permalink

This topic has been resolved and has been closed to new replies.