Make WordPress Core

Changeset 5593


Ignore:
Timestamp:
05/29/2007 09:27:49 PM (16 years ago)
Author:
ryan
Message:

get_term_children() and category query fixes.

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/query.php

    r5592 r5593  
    845845            $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) ";
    846846            $cat_array = preg_split('/[,\s]+/', $q['cat']);
    847             $in_cats = $out_cats = $out_posts = '';
     847            $in_cats = $out_cats = array();
     848            $include_cats = $exclude_cats = '';
    848849            foreach ( $cat_array as $cat ) {
    849850                $cat = intval($cat);
    850                 $in = (strpos($cat, '-') !== false) ? false : true;
    851                 $cat = trim($cat, '-');
    852                 // TODO make an array, not a string, for out_cats.  use get_term_children()
    853                 if ( $in )
    854                     $in_cats .= "$cat, " . get_category_children($cat, '', ', ');
    855                 else
    856                     $out_cats .= "$cat, " . get_category_children($cat, '', ', ');
    857             }
    858             $in_cats = substr($in_cats, 0, -2);
    859             $out_cats = substr($out_cats, 0, -2);
    860             if ( strlen($in_cats) > 0 )
    861                 $in_cats = " AND $wpdb->term_taxonomy.term_id IN ({$q['cat']}) ";
    862             if ( strlen($out_cats) > 0 ) {
    863                 // TODO use get_objects_in_term
     851                $in = ($cat > 0);
     852                $cat = abs($cat);
     853                if ( $in ) {
     854                    $in_cats[] = $cat;
     855                    $in_cats = array_merge($in_cats, get_term_children($cat, 'category'));
     856                } else {
     857                    $out_cats[] = $cat;
     858                    $out_cats = arry_merge($out_cats, get_term_children($cat, 'category'));
     859                }
     860            }
     861            if ( ! empty($in_cats) ) {
     862                $include_cats = "'" . implode("', '", $in_cats) . "'";
     863                $include_cats = " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
     864            }
     865
     866            if ( !empty($out_cats) ) {
    864867                $ids = get_objects_in_terms($out_cats, 'category');
    865868                if ( is_array($ids) && count($ids > 0) ) {
    866                     foreach ( $ids as $id )
    867                         $out_posts .= "$id, ";
    868                     $out_posts = substr($out_posts, 0, -2);
     869                    $out_posts = "'" . implode("', '", $ids) . "'";
     870                    $exclude_cats = " AND $wpdb->posts.ID NOT IN ($out_posts)";
    869871                }
    870                 if ( strlen($out_posts) > 0 )
    871                     $out_cats = " AND $wpdb->posts.ID NOT IN ($out_posts)";
    872                 else
    873                     $out_cats = '';
    874872            }
    875873            $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
    876             $whichcat .= $in_cats . $out_cats;
     874            $whichcat .= $include_cats . $exclude_cats;
    877875            $groupby = "{$wpdb->posts}.ID";
    878876        }
  • trunk/wp-includes/taxonomy.php

    r5592 r5593  
    657657}
    658658
     659function get_term_children( $term, $taxonomy ) {
     660    $terms = _get_term_hierarchy($taxonomy);
     661
     662    if ( ! isset($terms[$term]) )
     663        return array();
     664
     665    $children = $terms[$term];
     666
     667    foreach ( $terms[$term] as $child ) {
     668        if ( isset($terms[$child]) )
     669            $children = array_merge($children, get_term_children($child, $taxonomy));
     670    }
     671
     672    return $children;
     673}
     674
    659675function clean_term_cache($ids, $taxonomy) {
    660676    if ( !is_array($ids) )
Note: See TracChangeset for help on using the changeset viewer.