7

I'm trying to hide a large amount of categories from my category widget. I've tried a few plugins but none of them seem to want to let me use the dropdown option. I looked into the widget_categories_args hook and that seems to be what I want but I can't get it to work.

Anyways here's my code

function widget_categories_args_filter( $cat_args ) {
$exclude_arr = array( 57,61,63,56,55,62,52,53,54,67,65 );

if( isset( $cat_args['exclude'] ) && !empty( $cat_args['exclude'] ) )
    $exclude_arr = array_unique( array_merge( explode( ',', $cat_args['exclude'] ), $exclude_arr ) );
$cat_args['exclude'] = implode( ',', $exclude_arr );
return $cat_args;
}

add_filter( 'widget_categories_args', 'widget_categories_args_filter', 10, 1 );

I Took that from here: https://codex.wordpress.org/Plugin_API/Filter_Reference/widget_categories_args

I'm putting this in my theme's functions.php. That shouldn't matter even though the function is set for plugins, right?

  • Try increasing the fourth number to some thing high like 1000 – s_ha_dum Jun 19 '15 at 21:35
  • This works for me for the bundled category widget. – s_ha_dum Jun 19 '15 at 23:54
  • @s_ha_dum the fourth number? In the add_filter() line? – FranticJ3 Jun 22 '15 at 14:21
  • Sorry, the fourth argument. You have 1. Try 1000. As I said, this works for me so there may be another function interfering. Setting the priority high might give yours the last word. Are you using the Core category widget? – s_ha_dum Jun 22 '15 at 14:25
  • @s_ha_dum yes I'm using the core categories widget. I set it at 10000 and it is still not hiding them. – FranticJ3 Jun 22 '15 at 14:58
7

I know this post is pretty old, but because I came across the same issue and this post came up higher than one with a solution, I figured I'd add this, which worked for me.

Source: http://coffeecupweb.com/how-to-exclude-or-hide-categories-from-category-widget-in-wordpress-sidebar/

//Hide categories from WordPress category widget
function exclude_widget_categories($args){
    $exclude = "1,4,8,57,80";
    $args["exclude"] = $exclude;
    return $args;
}
add_filter("widget_categories_args","exclude_widget_categories");
  • 1
    I can verify this works great in March 2018! Bonus tip: hover over a category on the the Categories page and looks for the "tag_id" in the link at the bottom of your browser. Yeah, I know it says tag_id, but it still is referring to the category. – Eric Hepperle - CodeSlayer2010 Mar 3 '18 at 20:37
1

Hide the uncatecogrized / default category in WooCommerce:

I guess I'm not the only one who came to this page in search for a way to hide the default / uncategorized category introduced in WooCommerce 3.3.

If you're one of them, instead of hard-coding the category ID which may be different in different environments / installations, you can use the following snippet, modified from the snippet by Mike Jolley to hide it from the Woocommerce Product Categories widget:

<?php // Do not include this if already open!

/**
 * Code goes in theme functions.php.
 *
 * If you use dropdown instead of hierachical view, 
 * hook to the following filter instead: 
 *      `woocommerce_product_categories_widget_dropdown_args`
 */
add_filter( 'woocommerce_product_categories_widget_args', 'custom_woocommerce_product_categories_widget_args' );

function custom_woocommerce_product_categories_widget_args( $args ) {
  $args['exclude'] = get_option( 'default_product_cat' );
  return $args;
}
0

This works: https://gist.github.com/peltopiri/76e7d1143e33b424633114103cfae5ec

<?php
function exclude_woocommerce_widget_product_categories($widget_args) {
    //Insert excluded category ids here
    $excludes = array(12,33);
    $includes = explode(",",$widget_args['include']);

    $includes = array_filter($includes, function($value) use ($excludes) {
      return !in_array($value, $excludes);
    });
    $widget_args["include"] = implode(",", $includes);
    return $widget_args;
}
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'exclude_woocommerce_widget_product_categories');
add_filter( 'woocommerce_product_categories_widget_args', 'exclude_woocommerce_widget_product_categories');
0

After several searches and tests, hiding post categories for list items and drop-down is done separately since they're called by different filter ids.

For list category items use

add_filter("widget_categories_args","YOUR_CUSTOM_FUNCTION");

For drop-down category items use

add_filter("widget_categories_dropdown_args","YOUR_CUSTOM_FUNCTION");

Reference: https://basicwp.com/exclude-categories-from-category-widgets-in-wordpress/

protected by Community Mar 24 at 7:56

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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