2

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'?

4

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...
}
  • It seems to works for everything except when something is only tagged 'Uncategorized'... then it shows every category, including Uncategorized. Dang. – Django Reinhardt Sep 23 '16 at 11:54
  • 1
    yes, that's expected, hence my comment about checking if it's empty or not. It's the same with WP_Query, we have to check for empty arrays before assigning to the arguments. @DjangoReinhardt – birgire Sep 23 '16 at 12:06
  • I updated the answer to make to more clear @DjangoReinhardt – birgire Sep 23 '16 at 12:28
2

I build some tricky code to exclude category having ID 1. I'm trying to exclude category(ID:1) even remove from pluck also. Your code has include and exclude both parameter and this conflict the result.

//List the pluck...
$exclude_cat_id = 1;
$list_pluck = wp_list_pluck(get_the_category(), 'term_id');

//Get exlude pluck(ID:1) index...
$exclude_pluck = array_search($exclude_cat_id, $list_pluck);

//unset excluded pluck...
unset($list_pluck[$exclude_pluck]);

//Get all category except ID=1
$arrCat = wp_list_categories([
  'include' => $list_pluck,
  'title_li' => '',
  'exclude' => array($exclude_cat_id),
  'exclude_tree' => array($exclude_cat_id),
]);

Hope this help you well!

1

If my case, the only time I didn't want the list of categories to appear was if a post was 'Uncategorized'. The simplest solution in the end was just to use in_category():

if (!in_category(1)) {
    // Display the categories this post belongs to, as links
    wp_list_categories([
            'include' => wp_list_pluck(get_the_category(), 'term_id'),
            'title_li' => ''
    ]);
}
0
   $exclude = array();
   foreach (get_categories() as $category) 
   {$exclude[] = 1;}
   if (! empty($exclude)) 
   { $args .= ('' === $args) ? '' : '&';$args .= exclude='.implode(',', $exclude);}
   wp_list_categories($args);
  • 1
    Explanation for the code would be great! – bravokeyl Sep 23 '16 at 13:38
  • just put in your functions.php file.. – rajnik faldu Sep 23 '16 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.