Untitled
Anonymous
plain_text
08/22/2026 2:00 PM
2.7 KB
14
Indexable
add_action( 'transition_post_status', function( $new_status, $old_status, $post ) {
// Only run when a B2BKing group is published for the first time.
if (
'b2bking_group' !== $post->post_type ||
'publish' !== $new_status ||
'publish' === $old_status ||
metadata_exists( 'post', $post->ID, '_b2bking_category_visibility_initialized' )
) {
return;
}
/*
* Exact slug of the protected parent product category.
* Change this only if the category uses a different slug.
*/
$broker_parent = get_term_by(
'slug',
'broker-partners',
'product_cat'
);
/*
* Fail safely if the protected category cannot be found.
* No category visibility will be changed.
*/
if ( ! ( $broker_parent instanceof WP_Term ) ) {
return;
}
// Find every descendant of Broker Partners, at every hierarchy level.
$brokerage_category_ids = get_term_children(
$broker_parent->term_id,
'product_cat'
);
if ( is_wp_error( $brokerage_category_ids ) ) {
return;
}
$brokerage_category_ids = array_map( 'intval', $brokerage_category_ids );
$brokerage_category_ids[] = (int) $broker_parent->term_id;
// Retrieve all WooCommerce product categories, including empty ones.
$category_ids = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'fields' => 'ids',
)
);
if ( is_wp_error( $category_ids ) ) {
return;
}
foreach ( $category_ids as $category_id ) {
$is_brokerage_category = in_array(
(int) $category_id,
$brokerage_category_ids,
true
);
/*
* Standard categories receive visibility value 1.
* Brokerage categories explicitly receive visibility value 0.
*/
update_term_meta(
$category_id,
'b2bking_group_' . $post->ID,
$is_brokerage_category ? 0 : 1
);
}
// Ensure this operation cannot run again for the same group.
update_post_meta(
$post->ID,
'_b2bking_category_visibility_initialized',
1
);
// Clear B2BKing visibility caches and transients.
if ( function_exists( 'b2bking' ) ) {
b2bking()->clear_caches_transients();
}
}, 20, 3 );Editor is loading...
Leave a Comment