Support » Fixing WordPress » List of top-level/root categories

  • Resolved jeremyduffy


    All I want is to be able to easily get the top-level categories from my page. In this case, any that has no parent. Isn’t there a simple way to do this without wp_list_categories?

    Alternatively, is there a way to get wp_list_categories to stop returning all that data and just give me the IDs?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Actually, I think I’ve got it. My query was working fine, I just forgot to exclude tags in my search. Here’s what worked:

    $result = $wpdb->get_results(“SELECT, t.term_id, tt.count, slug FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.parent=’0′ AND tt.count > 0 AND tt.taxonomy=’category’ ORDER BY ASC”);

    Returns categories with no parent that include at least one child



    excellent code!
    thank you so much

    how can i exclude some categories from that result?

    Moderator Michael Adams (mdawaffe)



    $root_categories = get_categories( array(
    	'parent' => 0,
    ) );

    To exclude categories, you can do:

    $root_categories = get_categories( array(
    	'parent' => 0,
    	'exclude' => array( 1, 2, 3 ),
    ) );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘List of top-level/root categories’ is closed to new replies.