WordPress.org

Forums

[resolved] List of top-level/root categories (4 posts)

  1. jeremyduffy
    Member
    Posted 7 years ago #

    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?

  2. jeremyduffy
    Member
    Posted 7 years ago #

    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.name, 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 t.name ASC");

    Returns categories with no parent that include at least one child

  3. dejanc
    Member
    Posted 6 years ago #

    excellent code!
    thank you so much

    how can i exclude some categories from that result?

  4. Michael Adams (mdawaffe)
    Member
    Posted 5 years ago #

    $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 ),
    ) );

Topic Closed

This topic has been closed to new replies.

About this Topic