# Categories Widget: Top Level Only, Busiest First

> Makes the Categories widget list only top-level categories and sorts them by number of posts, so a long category tree becomes a short list of your main sections.

- Type: PHP
- Runs: Run everywhere
- Category: [Widgets](https://wpide.com/library/category/widgets/)
- Tags: widgets, categories, sorting
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/categories-widget-top-level-by-count/

## Code

```php
// true = list only top-level categories (no subcategories).
$wpide_cat_widget_top_level = true;
// true = order categories by number of posts, busiest first, instead of A to Z.
$wpide_cat_widget_by_count = true;

$wpide_cat_widget_args = function ( $args ) use ( $wpide_cat_widget_top_level, $wpide_cat_widget_by_count ) {
    if ( $wpide_cat_widget_top_level ) {
        $args['depth']  = 1;
        $args['parent'] = 0;
    }

    if ( $wpide_cat_widget_by_count ) {
        $args['orderby'] = 'count';
        $args['order']   = 'DESC';
    }

    return $args;
};

add_filter( 'widget_categories_args', $wpide_cat_widget_args );
add_filter( 'widget_categories_dropdown_args', $wpide_cat_widget_args );
```

## Notes

Switch either setting off at the top of the snippet; works for both the list and the dropdown version of the widget.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Categories Widget: Top Level Only, Busiest First" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
