Support

Account

Home Forums Bug Reports get_fields() not working with fields named as number

Helping

get_fields() not working with fields named as number

  • Hi there,

    I’m using the PRO version of ACF 5.6.8 on WP 4.9.4 and I have some problems using get_fields($post->ID) with fields called with numbers (for example 1, 2, 3), because it return false instead of an array.
    While using get_field(‘1’, $post->ID) it works as expeted.

    Below the json exported from ACF:

    
    [
        {
            "key": "group_5a89d694c5c66",
            "title": "Home",
            "fields": [
                {
                    "key": "field_5a89d69fdcf4f",
                    "label": "1",
                    "name": "",
                    "type": "tab",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "placement": "top",
                    "endpoint": 0
                },
                {
                    "key": "field_5a89d6e453faa",
                    "label": "1",
                    "name": "1",
                    "type": "group",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "layout": "block",
                    "sub_fields": [
                        {
                            "key": "field_5a89d6e453fab",
                            "label": "Title",
                            "name": "title",
                            "type": "text",
                            "instructions": "",
                            "required": 1,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "",
                            "prepend": "",
                            "append": "",
                            "maxlength": ""
                        },
                        {
                            "key": "field_5a89d70259e4d",
                            "label": "Description",
                            "name": "description",
                            "type": "wysiwyg",
                            "instructions": "",
                            "required": 1,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "tabs": "all",
                            "toolbar": "basic",
                            "media_upload": 0,
                            "delay": 0
                        }
                    ]
                },
                {
                    "key": "field_5a89d6b6dcf50",
                    "label": "2",
                    "name": "",
                    "type": "tab",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "placement": "top",
                    "endpoint": 0
                },
                {
                    "key": "field_5a89d6c353fa8",
                    "label": "2",
                    "name": "2",
                    "type": "group",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "layout": "block",
                    "sub_fields": [
                        {
                            "key": "field_5a89d6d353fa9",
                            "label": "Title",
                            "name": "title",
                            "type": "text",
                            "instructions": "",
                            "required": 1,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "",
                            "prepend": "",
                            "append": "",
                            "maxlength": ""
                        }
                    ]
                }
            ],
            "location": [
                [
                    {
                        "param": "page_template",
                        "operator": "==",
                        "value": "page-templates\/home.php"
                    }
                ]
            ],
            "menu_order": 0,
            "position": "normal",
            "style": "default",
            "label_placement": "top",
            "instruction_placement": "label",
            "hide_on_screen": "",
            "active": 1,
            "description": ""
        }
    ]
    

    At the moment I’ve solved using different name (for example first instead of 1).

    Thank you,
    Roberto

  • The short answer: Field names cannot be numbers.

    The long answer:

    I suspect that your call to get_fields() when you have fields with numbers as names is causing a php error.

    ACF uses the equivalent of get_post_meta($post_id) a core WP function to get all the meta values. This returns an array of meta_key => meta_value pairs. It then loops through these results using

    
    foreach( $meta as $k => $v ) {
    

    This is a standard way to loop through an array where you need the index and value. This creates sets $k to 1, which is then translated into the varialble $1. In PHP, variables that start with a number are syntax errors.

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

The topic ‘get_fields() not working with fields named as number’ is closed to new replies.