Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.