I had an error in the above code. $key used in both for loops.
Working code for filtering empty product categories out of menu items:
function exclude_empty_cat_menu_items( $items, $menu, $args ) {
// Get a list of product categories that excludes empty categories
$non_empty_categories = get_categories(array('taxonomy' => 'product_cat'));
// Iterate over the menu items
foreach ( $items as $key => $item ) {
$is_empty = true;
// check current item id is in the non-empty categories array
foreach ( $non_empty_categories as $key2 => $cat )
if ($item->title === $cat->name)
$is_empty = false;
// if it is empty remove it from array
if ($is_empty) unset($items[$key]);
}
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'exclude_empty_cat_menu_items', null, 3 );
Related Posts:
- Reflect nested categories in appearance > menus
- Display WooCommerce product categories in a 4 columns custom menu [closed]
- Add Product categories to WordPress menu without losing hierarchy
- Building Menu from Woocommerce Product Categories
- List a maximum of ‘x’ product categories with qty of ‘y’ in each column
- Woocommerce filter function not updating
- Show different menu on each woocommerce category page
- How to remove an item from a custom Walker_Nav_Menu
- How to add all subcategories as submenu in WordPress menu
- WordPress category title not update in navigation menu
- How can I add a Categories page link to a menu? [closed]
- Update wordpress menu with woocommerce products
- Retrieve Product category ‘NAME’ by product category ID – WooCommerce?
- Exclude the category from the WordPress loop
- Exclude categories from Loop, queries, widgets, post navigation
- Programmatically create product category and add thumbnail in woocommerce
- List posts by category exclude current post
- wp_list_categories exclude not working
- Category menu item and its last 10 posts as sub-menu
- Keep active class in post page
- Use get_term_children to get the sub category of a parent category for the current post
- Hide uncategorized products from the shop page
- Remove word “Category” from WooCommerce product page [closed]
- Filtering search results
- WooCommerce change category url and product base
- Output link to category from WP_Query loop of woocommerce products
- How to get product count with respect to categories in WooComerce
- Get woocommerce catogry
- How to exclude posts from a category when using this particular format
- Exclude category from Tag Template
- exclude post from displaying in loop if it is in a category, but not in many categories
- Set post categories to include parents when setting child category
- Linking products to categories
- Warning: sizeof (): Parameter must be an array or an object that implements Countable, On products pages
- Omit specific product categories from WooCommerce shortcode
- Nav menu category links not showing
- Multiple Domain Names – One WP Install (non-Multisite) – Default Each Domain name to Product Category WooCommerce
- Exclude Posts From Specific Category from Next and Previous post links
- Hard coded main navigation
- Query Posts Exclude Entire Category
- Exclude category from fucntion
- Class active for Archive link – looped out
- Hide woocommerce category name from specific pages
- need to add custom field in more products
- How to redefine the sorting of product categories by menu_order?
- Pagination is not working properly in Product Category/Tag pages
- How to recreate a copy of a product category in WooCommerce? [closed]
- Get parrent category name
- Sorting category products not working
- WordPress category widget only show categories with children
- Woocommerce custom category page with pagination [closed]
- Menus don’t work after 4.4 [closed]
- Create menu / submenu from category subcategory and posts
- Redirect to another page using contact form 7? [closed]
- wp_list_pages() exclude category
- Admin: Navigation Menus do not show empty categories/taxonomies in the list’s search tab
- Give Children Unique ID’s
- Do not show sub categories in the loop on archive-product.php
- why get_the_post_thumbnail( the_ID()) echos extra post id
- Filter WooCommerce archive pages by an additional category
- Custom tax_query filter not working for Woocommerce product categories
- How do order product categories – on a parent category page – in Woocommerce?
- Two Sections on Woocommerce Catergory Page [closed]
- Woocommerce search form with category select
- wp_list_categories not excluing multiple ids
- How to Sync Menu, Widgets and other masters from Main Website to its Sub Site
- Exclude category from foreach loop
- Theme modification for custom category menus
- Exclude the category in WordPress
- Is custom menu name and category name linked in wordpress?
- How to automatically generate custom menu item from categories?
- WooCommerce shared categories
- WP-CLI Add category as menu item main-menu
- Displaying Single Product Page Parent and Child Category
- Conditional label Woocommerce archive [unsolved]
- How to show given products only using product id array from specific category page in woocommerce
- WooCommerce REST API not returning the default products category
- How to show WooCommerce Categories on ‘shop’ page instead of products?
- Exclude a category and post_type from wp_query
- Get WooCommerce product category list in functions.php
- Is there a reason to put a product in both the Child and Parent Categories?
- Dynamically display sub-categories on primary menu without adding them
- WooCommerce showing active product categories on single product page
- Woocommerce product catalog, products with different description
- set a parent category in a product woocommerce
- No results found for second level category
- How to display products with multiple conditions, product_id and category_id
- How to get related category/categories in WordPress archive page
- Hide category links if there is nothing in the categories – Main Navigation submenu item
- woocommerce – products by category slug? [closed]
- Right way for setting categories
- Make three menus with three category levels
- How to have a category not show up in query post with page panigation?
- Remove the current item from a menu
- Customize category query in widget
- Chained drop-down menu for custom taxonomy using chained.js
- Excluding cateory not working on my site. how to solve this? [closed]
- Get all categories
- Woocommerce Force the category choice before creating new product? [duplicate]
- Function to reference post meta in place of category Woocommerce