From 933e5115185178d75da16ab36498080d826edf70 Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 15 Jan 2010 17:40:37 +0000 Subject: [PATCH] Support id=>parent in fields arg of get_terms. Returns array of parent ids keyed by the id of the child term. git-svn-id: http://svn.automattic.com/wordpress/trunk@12729 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/taxonomy.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 0d5bce480..521d06ead 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -797,11 +797,11 @@ function &get_terms($taxonomies, $args = '') { $selects = array(); if ( 'all' == $fields ) $selects = array('t.*', 'tt.*'); - else if ( 'ids' == $fields ) + else if ( 'ids' == $fields || 'id=>parent' == $fields ) $selects = array('t.term_id', 'tt.parent', 'tt.count'); else if ( 'names' == $fields ) $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name'); - $select_this = implode(', ', apply_filters( 'get_terms_fields', $selects, $args )); + $select_this = implode(', ', apply_filters( 'get_terms_fields', $selects, $args )); $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $limit"; @@ -844,7 +844,11 @@ function &get_terms($taxonomies, $args = '') { reset ( $terms ); $_terms = array(); - if ( 'ids' == $fields ) { + if ( 'id=>parent' == $fields ) { + while ( $term = array_shift($terms) ) + $_terms[$term->term_id] = $term->parent; + $terms = $_terms; + } elseif ( 'ids' == $fields ) { while ( $term = array_shift($terms) ) $_terms[] = $term->term_id; $terms = $_terms; @@ -2066,10 +2070,10 @@ function _get_term_hierarchy($taxonomy) { return $children; $children = array(); - $terms = get_terms($taxonomy, array('get' => 'all', 'orderby' => 'id', 'fields' => 'ids')); - foreach ( $terms as $term ) { - if ( $term->parent > 0 ) - $children[$term->parent][] = $term->term_id; + $terms = get_terms($taxonomy, array('get' => 'all', 'orderby' => 'id', 'fields' => 'id=>parent')); + foreach ( $terms as $term_id => $parent ) { + if ( $parent > 0 ) + $children[$parent][] = $term_id; } set_transient("{$taxonomy}_children", $children);