From cfa5ecfd4b61939351a60b8547c2661e3ea25fdf Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 29 May 2007 21:27:49 +0000 Subject: [PATCH] get_term_children() and category query fixes. git-svn-id: https://develop.svn.wordpress.org/trunk@5593 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/query.php | 42 +++++++++++++++++++--------------------- wp-includes/taxonomy.php | 16 +++++++++++++++ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index 58f32a3a01..903b52838e 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -844,36 +844,34 @@ class WP_Query { $q['cat'] = addslashes_gpc($q['cat']); $join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) "; $cat_array = preg_split('/[,\s]+/', $q['cat']); - $in_cats = $out_cats = $out_posts = ''; + $in_cats = $out_cats = array(); + $include_cats = $exclude_cats = ''; foreach ( $cat_array as $cat ) { $cat = intval($cat); - $in = (strpos($cat, '-') !== false) ? false : true; - $cat = trim($cat, '-'); - // TODO make an array, not a string, for out_cats. use get_term_children() - if ( $in ) - $in_cats .= "$cat, " . get_category_children($cat, '', ', '); - else - $out_cats .= "$cat, " . get_category_children($cat, '', ', '); + $in = ($cat > 0); + $cat = abs($cat); + if ( $in ) { + $in_cats[] = $cat; + $in_cats = array_merge($in_cats, get_term_children($cat, 'category')); + } else { + $out_cats[] = $cat; + $out_cats = arry_merge($out_cats, get_term_children($cat, 'category')); + } } - $in_cats = substr($in_cats, 0, -2); - $out_cats = substr($out_cats, 0, -2); - if ( strlen($in_cats) > 0 ) - $in_cats = " AND $wpdb->term_taxonomy.term_id IN ({$q['cat']}) "; - if ( strlen($out_cats) > 0 ) { - // TODO use get_objects_in_term + if ( ! empty($in_cats) ) { + $include_cats = "'" . implode("', '", $in_cats) . "'"; + $include_cats = " AND $wpdb->term_taxonomy.term_id IN ($include_cats) "; + } + + if ( !empty($out_cats) ) { $ids = get_objects_in_terms($out_cats, 'category'); if ( is_array($ids) && count($ids > 0) ) { - foreach ( $ids as $id ) - $out_posts .= "$id, "; - $out_posts = substr($out_posts, 0, -2); + $out_posts = "'" . implode("', '", $ids) . "'"; + $exclude_cats = " AND $wpdb->posts.ID NOT IN ($out_posts)"; } - if ( strlen($out_posts) > 0 ) - $out_cats = " AND $wpdb->posts.ID NOT IN ($out_posts)"; - else - $out_cats = ''; } $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' "; - $whichcat .= $in_cats . $out_cats; + $whichcat .= $include_cats . $exclude_cats; $groupby = "{$wpdb->posts}.ID"; } diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index e3f29cac06..3ddcfd3fb8 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -656,6 +656,22 @@ function get_term_by($field, $value, $taxonomy, $output = OBJECT) { } } +function get_term_children( $term, $taxonomy ) { + $terms = _get_term_hierarchy($taxonomy); + + if ( ! isset($terms[$term]) ) + return array(); + + $children = $terms[$term]; + + foreach ( $terms[$term] as $child ) { + if ( isset($terms[$child]) ) + $children = array_merge($children, get_term_children($child, $taxonomy)); + } + + return $children; +} + function clean_term_cache($ids, $taxonomy) { if ( !is_array($ids) ) $ids = array($ids);