Support

Account

Home Forums General Issues How to check if field group exists in ACF5/Pro

Solved

How to check if field group exists in ACF5/Pro

  • Hi Elliot / Anyone,

    Is their a way to check if the field group is existing on ACF5? I did have a working code for ACF4 but when I migrated to ACF5 it is not working. I suspect that because of ACF5 have change its post type from “acf” to new db table.

    See code below: (working in acf4)

    
    if (!get_page_by_title('Field Group Name', OBJECT, 'acf')) :
            
    // Load the ACF php
            require_once (dirname(__FILE__) . '/includes/ACF5/acf-custom-field-group.php');
        endif;
  • I have found this solution but it leads to another issue.

    Read code below

    class helper{
    //helper function
     public static function is_field_group_exists($value, $type='title') {
            $exists = false;
    
            if ($field_groups = acf_get_field_groups()) {
                foreach ($field_groups as $field_group) {
                    if ($field_group[$type] == $value) {
                        $exists = true;
                    }
                }
            }
            return $exists;
        }
    }
    
    //use
    if (!helper::is_field_group_exists('Field Group Title')) :
    // load acf exported php
            require_once (dirname(__FILE__) . '/includes/' . $acf_version . '/acf-field-group-exported.php');
        endif;
    

    Source: ACF5 field group exist

  • ACF stores field groups in the wp_posts table now. So you can use the post ID to check if it exists, or any other valid way to look for a post.

  • Thanks Daron it did give me an idea.

    Here what I come up with the idea.

    
    function is_field_group_exists($value, $type='post_title') {
            $exists = false;
            if ($field_groups = get_posts(array('post_type'=>'acf-field-group'))) {
                foreach ($field_groups as $field_group) {
                    if ($field_group->$type == $value) {
                        $exists = true;
                    }
                }
            }
            return $exists;
        }

    Anyone can use the function for checking if field group exist.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.