[wp-trac] [WordPress Trac] #12898: Option in query_posts to include parent and/or grandparent categories

WordPress Trac wp-trac at lists.automattic.com
Wed Apr 7 18:57:22 UTC 2010


#12898: Option in query_posts to include parent and/or grandparent categories
---------------------------------------+------------------------------------
 Reporter:  vlasky                     |        Owner:  ryan      
     Type:  feature request            |       Status:  closed    
 Priority:  normal                     |    Milestone:            
Component:  Query                      |      Version:  3.0       
 Severity:  normal                     |   Resolution:  worksforme
 Keywords:  category, parent category  |  
---------------------------------------+------------------------------------
Changes (by scribu):

  * status:  new => closed
  * resolution:  => worksforme
  * milestone:  3.0 =>


Comment:

 Please post on the [http://wordpress.org/extend/ideas/ Ideas Forum] before
 opening feture requests tickets.

 Adding a 'level' argument to query_posts() is unnecessary. You just need
 to find the parent category and call query_posts() with that. Here's an
 example function:

 {{{
 function get_term_parent($term, $level = 1) {
         $taxonomy = $term->taxonomy;

         if ( !is_taxonomy_hierarchical($taxonomy) )
                 return false;

         $parents = get_terms($taxonomy, array('get' => 'all', 'orderby' =>
 'id', 'fields' => 'id=>parent'));

         $term_id = $term->term_id;

         while ( $level-- ) {
                 if ( $parents[$term_id] > 0 )
                         $term_id = $parents[$term_id];
                 else
                         break;
         }

         return get_term($term_id, $taxonomy);
 }
 }}}

 Example usage:

 {{{
 $cat_obj = get_term_by('slug', 'germany', 'category');
 $grandpa = get_term_parent($cat_obj, 2);

 query_posts(array('cat' => $grandpa->term_id));
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/12898#comment:1>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list