Make WordPress Core

Opened 19 years ago

Closed 19 years ago

Last modified 8 years ago

#48 closed defect (bug) (fixed)

Incorrect functionality in get_nested_categories() in admin-functions.php

Reported by: ophois's profile ophois Owned by: jasonv's profile JasonV
Milestone: Priority: normal
Severity: minor Version:
Component: Administration Keywords:
Focuses: Cc:


This function will fail for the case where you have a hirerchy
and b's id is lower than a's. In this case the category c (and its siblings) will not get added on to the list of categories returned. This can occur when categories have been moved and deleted lots. This will stop c (and its siblings) being visible in the write / edit post categories list.

I have a fix for this (see later) can one of the devs look at it and see if they want to merge it in to the code base (see additional info).

Change History (6)

#2 @JasonV
19 years ago

  • Owner changed from anonymous to JasonV
  • Status changed from new to assigned

#3 @ophois
19 years ago

Hmmm, turns out my suggested fix don't work. Missed a line from second function:

function get_nested_categories($default = 0, $parent = 0) {

global $post_ID, $tablecategories, $tablepost2cat, $mode, $wpdb;

if ($post_ID) {

$checked_categories = $wpdb->get_col("

SELECT category_id
FROM $tablecategories, $tablepost2cat
WHERE $tablepost2cat.category_id = cat_ID AND $tablepost2cat.post_id = '$post_ID'

if(count($checked_categories) == 0)

No selected categories, strange
$checked_categories[] = $default;


} else {

$checked_categories[] = $default;


$cats = return_categories_list($parent, TRUE);
$result = array();

foreach($cats as $cat)

$result[$cat]children? = get_nested_categories($default, $cat);
$result[$cat]cat_ID? = $cat;
$result[$cat]checked? = in_array($cat, $checked_categories);
$result[$cat]cat_name? = stripslashes(get_the_category_by_ID($cat));


return $result;


Also added guard against the post having no catagories assigned to it.

#4 @JasonV
19 years ago

  • Resolution changed from 10 to 20
  • Status changed from assigned to closed

ophois: Thanks for the fix. I only changed things a little bit because variables like $tablecategories have been renamed $wpdb->categories...

This ticket was mentioned in Slack in #feature-respimg by joemcgill. View the logs.

8 years ago

Note: See TracTickets for help on using tickets.