Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shameemreza/2ebcfdb628f83e01d012e288aee0e46e to your computer and use it in GitHub Desktop.
Save shameemreza/2ebcfdb628f83e01d012e288aee0e46e to your computer and use it in GitHub Desktop.
Force a recount of all brand terms in WooCommerce
/**
* Force a recount of all brand terms
*/
function recount_brand_terms() {
// Only run on the homepage
if ( !is_front_page() && !is_home() ) {
return;
}
// Check if the product_brand taxonomy exists
if ( !taxonomy_exists( 'product_brand' ) ) {
return;
}
// Get all brand terms
$brands = get_terms( array(
'taxonomy' => 'product_brand',
'hide_empty' => false,
'fields' => 'id=>parent',
) );
// Recount terms
if ( !empty( $brands ) && !is_wp_error( $brands ) ) {
_wc_term_recount( $brands, get_taxonomy( 'product_brand' ), true, false );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment