wp_list_categories exclude not working

The wp_list_categories() function uses get_terms() behind the scenes, where the documentation for the exclude argument says:

If $include is non-empty, $exclude is ignored.

Instead you could try to exclude the term_id from the include values:

$include = wp_filter_object_list( 
    get_the_category(),   // Data
    [ 'term_id' => 1 ],   // Filter Data
    'NOT',                // Filter Option (exclude)
    'term_id'             // Pluck Data 
);

where we use wp_filter_object_list() to both filter and pluck. In general it could be better to check if the $include array is empty or not:

if( $include )
{
    // ... stuff above ...

    wp_list_categories( [
        'include'  => $includes,
        'title_li' => '',
    ] );

    // ... stuff below...
}

Leave a Comment