I have seen a number of similar posts to this, but not one that specifically solves the issue I am having.
I need to create a function that allows me to remove empty tag archive pages from the tag cloud on my woocommerce site so they do not lead users onto empty pages.
The only code I have found that allows removal of the entire tag cloud is:
add_action( 'widgets_init', 'misha_remove_product_tag_cloud_widget' );
function misha_remove_product_tag_cloud_widget(){
unregister_widget('WC_Widget_Product_Tag_Cloud');
}
I believe this could be used in conjunction with something like this code which allows for the removal of all unused categories from various locations:
add_filter( 'wp_get_nav_menu_items', 'nav_remove_empty_category_menu_item', 10, 3 );
function nav_remove_empty_category_menu_item ( $items, $menu, $args ) {
global $wpdb;
$nopost = $wpdb->get_col( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE count = 0" );
foreach ( $items as $key => $item ) {
if ( ( 'taxonomy' == $item->type ) && ( in_array( $item->object_id, $nopost ) ) ) {
unset( $items[$key] );
}
}
return $items;
}
I am unsure how to work the two together, but the end result should allow for any unused product tag archive pages to be hidden from the tag cloud until they are either used again or removed entirely from the website - but always preventing users from accessing empty tag pages!
This question was answered in more depth over here: WooCommerce - How to hide product tags in the tag cloud when it has no 'in stock' products
The code used as the final solution is as follows: