[wp-hackers] categories with no childs

Charles K. Clarkson cclarkson at htcomp.net
Sat May 29 08:39:16 UTC 2010


capsx wrote:

: which is the easiest way to get wordpress categories with no child
: categories?

Please define "easiest way". :)

function get_childless_categories() {

     // Get all categories
     $categories = get_terms('category');

     // Get all parent ids
     $parents = array();
     foreach ( $categories as $category )
         $parents[] = $category->parent;

     // Make unique
     $parents = array_values(array_unique($parents));

     // Filter out childless categories
     $childless = array();
     foreach ( $categories as $category ) {
         if ( ! in_array($category->term_id, $parents) )
             $childless[] = $category;
     }

     return $childless;
}


HTH,

Charles Clarkson
-- 
Mobile Home Investor
Free Market Advocate
Programmer

I'm not really a smart person. I just play one on the Internet.

Stephenville, TX
+1 (254) 968-8328


More information about the wp-hackers mailing list