Advertisement
tollmanz

Custom Category Sort

Jul 1st, 2011
1,835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. <?php
  2. add_action('category_add_form_fields', 'category_metabox_add', 10, 1);
  3. add_action('category_edit_form_fields', 'category_metabox_edit', 10, 1);
  4. add_action('created_category', 'save_category_meta_data', 10, 1);  
  5. add_action('edited_category', 'save_category_meta_data', 10, 1);
  6.  
  7. function category_metabox_add($tag)
  8. {
  9. ?>
  10.     <div class="form-field">
  11.         <label for="tax-order"><?php _e('Order') ?></label>
  12.         <input name="tax-order" id="tax-order" type="text" value="" size="40" aria-required="true" />
  13.         <p class="description"><?php _e('Determines the order in which the term is displayed.'); ?></p>
  14.     </div>
  15. <?php
  16. }  
  17.  
  18. function category_metabox_edit($tag)
  19. {
  20. ?>
  21.     <tr class="form-field">
  22.         <th scope="row" valign="top">
  23.             <label for="tax-order"><?php _e('Order'); ?></label>
  24.         </th>
  25.         <td>
  26.             <input name="tax-order" id="tax-order" type="text" value="<?php echo get_term_meta($tag->term_id, 'tax-order', true); ?>" size="40" aria-required="true" />
  27.             <p class="description"><?php _e('Determines the order in which the term is displayed.'); ?></p>
  28.         </td>
  29.     </tr>
  30. <?php
  31. }
  32.  
  33. function save_category_meta_data($term_id)
  34. {
  35.     if (isset($_POST['tax-order']))
  36.         update_term_meta( $term_id, 'tax-order', $_POST['tax-order']);      
  37. }
  38.  
  39. add_filter('get_terms', 'custom_term_sort', 10, 3);
  40.  
  41. function custom_term_sort($terms, $taxonomies, $args)
  42. {      
  43.     // Controls behavior when get_terms is called at unusual times resulting in a terms array without objects
  44.     $empty = false;
  45.    
  46.     // Create collector arrays
  47.     $ordered_terms = array();
  48.     $unordered_terms = array();
  49.  
  50.     // Add taxonomy order to terms
  51.     foreach($terms as $term)
  52.     {
  53.         // Only set tax_order if value is an object
  54.         if(is_object($term))
  55.         {
  56.             if($taxonomy_sort = get_term_meta($term->term_id, 'tax-order', true))
  57.             {
  58.                 $term->tax_order = (int) $taxonomy_sort;
  59.                 $ordered_terms[] = $term;
  60.             }
  61.             else
  62.             {
  63.                 $term->tax_order = (int) 0;
  64.                 $unordered_terms[] = $term;
  65.             }
  66.         }
  67.         else
  68.             $empty = true;
  69.     }
  70.    
  71.     // Only sort by tax_order if there are items to sort, otherwise return the original array
  72.     if(!$empty && count($ordered_terms) > 0)
  73.         quickSort($ordered_terms);
  74.     else
  75.         return $terms;
  76.  
  77.     // Combine the newly ordered items with the unordered items and return
  78.     return array_merge($ordered_terms, $unordered_terms);  
  79. }
  80.  
  81. function quickSort(&$array)
  82. {
  83.     $cur = 1;
  84.     $stack[1]['l'] = 0;
  85.     $stack[1]['r'] = count($array)-1;
  86.    
  87.     do
  88.     {
  89.         $l = $stack[$cur]['l'];
  90.         $r = $stack[$cur]['r'];
  91.         $cur--;
  92.    
  93.         do
  94.         {
  95.             $i = $l;
  96.             $j = $r;
  97.             $tmp = $array[(int)( ($l+$r)/2 )];
  98.        
  99.             // partion the array in two parts.
  100.             // left from $tmp are with smaller values,
  101.             // right from $tmp are with bigger ones
  102.             do
  103.             {
  104.                 while( $array[$i]->tax_order < $tmp->tax_order )
  105.                 $i++;
  106.            
  107.                 while( $tmp->tax_order < $array[$j]->tax_order )
  108.                 $j--;
  109.            
  110.                 // swap elements from the two sides
  111.                 if( $i <= $j)
  112.                 {
  113.                      $w = $array[$i];
  114.                      $array[$i] = $array[$j];
  115.                      $array[$j] = $w;
  116.            
  117.                     $i++;
  118.                     $j--;
  119.                 }
  120.            
  121.             }while( $i <= $j );
  122.            
  123.             if( $i < $r )
  124.             {
  125.                 $cur++;
  126.                 $stack[$cur]['l'] = $i;
  127.                 $stack[$cur]['r'] = $r;
  128.             }
  129.             $r = $j;
  130.            
  131.         }while( $l < $r );
  132.            
  133.     }while( $cur != 0 );
  134. }
  135. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement