Make WordPress Core

Changeset 1432


Ignore:
Timestamp:
06/16/2004 04:40:40 PM (22 years ago)
Author:
jverber
Message:

Fix for bug #0000048: nested categories now display correctly in admin regardless of the order of their IDs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-functions.php

    r1429 r1432  
    1919}
    2020
    21 function get_nested_categories($default = 0) {
     21function return_categories_list( $parent = 0, $sortbyname = FALSE )
     22{
     23        /*
     24         * This function returns an list of all categories
     25         * that have $parent as their parent
     26         * if no parent is specified we will assume top level caegories
     27         * are required.
     28         */
     29        global $wpdb;
     30
     31        // select sort order
     32        $sort = "cat_id";
     33        if( TRUE == $sortbyname )
     34        {
     35                $sort = "cat_name";
     36        }
     37
     38        // First query the database
     39        $cats_tmp = $wpdb->get_results("SELECT cat_id FROM $wpdb->categories WHERE category_parent = $parent ORDER BY $sort");
     40
     41        // Now strip this down to a simple array of IDs
     42        $cats = array();
     43        if( count($cats_tmp) > 0 )
     44        {
     45                foreach( $cats_tmp as $cat )
     46                {
     47                        $cats[] = $cat->cat_id;
     48                }
     49        }
     50
     51        // Return the list of categories
     52        return $cats;
     53}
     54
     55function get_nested_categories($default = 0, $parent = 0) {
    2256 global $post_ID, $mode, $wpdb;
    2357
     
    2559   $checked_categories = $wpdb->get_col("
    2660     SELECT category_id
    27      FROM  $wpdb->categories, $wpdb->post2cat
     61     FROM $wpdb->categories, $wpdb->post2cat
    2862     WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID'
    2963     ");
     64
     65   if(count($checked_categories) == 0)
     66   {
     67     // No selected categories, strange
     68     $checked_categories[] = $default;
     69   }
     70
    3071 } else {
    3172   $checked_categories[] = $default;
    3273 }
    3374
    34  $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY category_parent DESC, cat_name ASC");
     75 $cats = return_categories_list($parent, TRUE);
    3576 $result = array();
    36  foreach($categories as $category) {
    37    $array_category = get_object_vars($category);
    38    $me = 0 + $category->cat_ID;
    39    $parent = 0 + $category->category_parent;
    40     if (isset($result[$me]))   $array_category['children'] = $result[$me];
    41    $array_category['checked'] = in_array($category->cat_ID, $checked_categories);
    42    $array_category['cat_name'] = stripslashes($category->cat_name);
    43    $result[$parent][] = $array_category;
     77
     78 foreach($cats as $cat)
     79 {
     80   $result[$cat]['children'] = get_nested_categories($default, $cat);
     81   $result[$cat]['cat_ID'] = $cat;
     82   $result[$cat]['checked'] = in_array($cat, $checked_categories);
     83   $result[$cat]['cat_name'] = stripslashes(get_the_category_by_ID($cat));
    4484 }
    4585
    46  return $result[0];
     86 return $result;
    4787}
    4888
Note: See TracChangeset for help on using the changeset viewer.