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.
Opens WPIDE on
Code
// 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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.