I’m try to exclude a specific category from a list of categories that a custom post has (in this case ‘Uncategorized’ – ID: 1).
I’ve tried exclude
:
wp_list_categories([
'include' => wp_list_pluck(get_the_category(), 'term_id'),
'title_li' => '',
'exclude' => 1
]);
But it still appears. How can I make sure it never appears, even if a post is tagged ‘Uncategorized’?
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...
}