Last active
November 8, 2025 12:34
-
-
Save mclarenmervin/2faf34df656275e9b96f99bfb55c0b99 to your computer and use it in GitHub Desktop.
add acf field group to specific product type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter('acf/get_field_groups', function ($groups) { | |
| // Only run in admin and on product edit screens | |
| if (!is_admin()) { | |
| return $groups; | |
| } | |
| $screen = get_current_screen(); | |
| if (empty($screen) || $screen->id !== 'product') { | |
| return $groups; | |
| } | |
| global $post; | |
| // Safety check | |
| if (!$post || get_post_type($post) !== 'product') { | |
| return $groups; | |
| } | |
| // Get WooCommerce product type safely | |
| $type = ''; | |
| if (function_exists('wc_get_product')) { | |
| $product = wc_get_product($post->ID); | |
| if ($product && method_exists($product, 'get_type')) { | |
| $type = $product->get_type(); | |
| } | |
| } | |
| // Fallback for new (unsaved) products | |
| if (empty($type) && isset($_GET['product_type'])) { | |
| $type = sanitize_text_field(wp_unslash($_GET['product_type'])); | |
| } | |
| // Only keep your group if product type is woosb | |
| foreach ($groups as $key => $group) { | |
| if ($group['key'] === 'group_690f32548cc33' && $type !== 'woosb') { | |
| unset($groups[$key]); // Remove this ACF group | |
| } | |
| } | |
| return $groups; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment