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.

Select dropdown with categories not ordering properly

  1. fu7u
    Member

    Hi,

    I have about 200 categories which are all of the municipalities in Sweden. Of some reason not all of my categories are ordered alphabetically. The first 21 items in the list are just random ones, and then the rest is ordered as they should. They are all child categories of a category called "Municipality".

    http://i44.tinypic.com/4qq89l.png

    I've tried running it on TwentyTwelve with all plugins deactivated, same result.

    Thanks.

    Posted 11 years ago on Monday April 29, 2013 | Permalink
  2. fu7u
    Member

    I checked my wp_term_taxonomy table and added up the amount of categories where post count is grater than 0, the result is also 21. So my guess is that categories with posts are being pushed up to the top of the list.

    Is there a way to make sure the dropdown always list categories in alphabetical order?

    Thanks.

    Posted 11 years ago on Monday April 29, 2013 | Permalink
  3. fu7u
    Member

    I've written this filter that sorts all my categories in the dropdown but still the same problem, anyone?

    add_filter('gform_pre_render_2', 'order_that_stuff');
    function order_that_stuff($form) {
    
      foreach ($form['fields'] as $field) {
        if ($field['type'] == 'post_category' && $field['inputType'] == 'select') {
    
          $sorted_choices = array();
    
          foreach ($field['choices'] as $choice) {
            $sorted_choices[] = $choice;
          }
    
          asort($sorted_choices);
          $field['choices'] = $sorted_choices;
    
          echo '<pre>';
          print_r($field);
          echo '</pre>';
        }
      }
    
      return $form;
    }
    Posted 11 years ago on Tuesday April 30, 2013 | Permalink
  4. fu7u
    Member

    PHP-version: 5.3.6
    MySQL-version: 5.5.9
    Wordpressversion: 3.5.1
    Gravity Forms Version: 1.7.2

    Posted 11 years ago on Tuesday April 30, 2013 | Permalink
  5. fu7u
    Member

    Solved! Here's my solution, please let me know if there's a better way of doing this:

    add_filter('gform_pre_render', 'order_that_stuff');
    function order_that_stuff($form) {
    
      foreach ($form['fields'] as $key => $field) {
        if ($field['type'] == 'post_category' && $field['inputType'] == 'select') {
    
          $sorted_choices = array();
    
          foreach ($field['choices'] as $choice) {
            $sorted_choices[] = $choice;
          }
    
          asort($sorted_choices);
          $form['fields'][$key]['choices'] = $sorted_choices;
    
        }
      }
    
      return $form;
    }
    Posted 11 years ago on Tuesday April 30, 2013 | Permalink
  6. David Peralty

    Looks great. All my best!

    Posted 11 years ago on Tuesday April 30, 2013 | Permalink

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