Home › Forums › Bug Reports › 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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
π Weβre excited to announce we've released Composer support for installing ACF PRO.
— Advanced Custom Fields (@wp_acf) January 31, 2023
π #ComposerPHP fans rejoice!
β¨ Please see the release post for all the details and full instructions. https://t.co/ebEfp61nlR
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.