Support

Account

Home Forums General Issues get_field_object in loop only returns first object, then Null

Solving

get_field_object in loop only returns first object, then Null

  • Good Morning Everybody

    This is my first time posting on here, and this might show:

    I want to return a field object in a loop using the name of the field.

    this is what my code looks like:

    $field_value = get_field_object($field[‘name’]);

    when I var_dump($field[‘name’]), it returns:

    \my_functions.php:9:string ‘performance’ (length=11)

    \my_functions.php:9:string ‘luxury’ (length=6)

    \my_functions.php:9:string ‘rarity’ (length=6)

    \my_functions.php:9:string ‘4_by_4_ability’ (length=14)

    \my_functions.php:9:string ‘age’ (length=3)

    \my_functions.php:9:string ‘average_mileage’ (length=15)

    \my_functions.php:9:string ‘price_of_parts’ (length=14)

    \my_functions.php:9:string ‘fuel_consumption’ (length=16)

    \my_functions.php:9:string ‘insurance’ (length=9)

    \my_functions.php:9:string ‘seats’ (length=5)

    \my_functions.php:9:string ‘boot_space’ (length=10)

    \my_functions.php:9:string ‘rear_seats’ (length=10)

    \my_functions.php:9:string ‘active_safety_systems’ (length=21)

    \my_functions.php:9:string ‘safety_rating’ (length=13)

    These values are correct, and when I enter the actual text manually in get_field_object, it returns the correct object.

    However, when enter get_field_object($field[‘name’]);, it only returns the first object i.e performance, but the rest of the objects are null.

    C:\Users\user\Local Sites\caracter-reviews-live\app\public\wp-
    content\themes\caracter_reviews_theme\my_functions.php:15:
    array (size=24)
    ‘ID’ => int 130
    ‘key’ => string ‘field_6142cacce9107’ (length=19)
    ‘label’ => string ‘Performance (0-100km/h)’ (length=23)
    ‘name’ => string ‘performance’ (length=11)
    ‘prefix’ => string ‘acf’ (length=3)
    ‘type’ => string ‘number’ (length=6)
    ‘value’ => string ‘6.2’ (length=3)
    ‘menu_order’ => int 0
    ‘instructions’ => string ” (length=0)
    ‘required’ => int 0
    ‘id’ => string ” (length=0)
    ‘class’ => string ” (length=0)
    ‘conditional_logic’ => int 0
    ‘parent’ => int 129
    ‘wrapper’ =>
    array (size=3)
    ‘width’ => string ” (length=0)
    ‘class’ => string ” (length=0)
    ‘id’ => string ” (length=0)
    ‘default_value’ => string ” (length=0)
    ‘placeholder’ => string ” (length=0)
    ‘prepend’ => string ” (length=0)
    ‘append’ => string ‘s’ (length=1)
    ‘min’ => string ” (length=0)
    ‘max’ => string ” (length=0)
    ‘step’ => string ” (length=0)
    ‘_name’ => string ‘performance’ (length=11)
    ‘_valid’ => int 1
    \my_functions.php:15:boolean false
    \my_functions.php:15:boolean false
    \my_functions.php:15:boolean false
    \my_functions.php:15:boolean false
    \my_functions.php:15:boolean false
    \my_functions.php:15:boolean false
    \my_functions.php:15:boolean false
    \my_functions.php:15:boolean false
    \my_functions.php:15:boolean false
    \my_functions.php:15:boolean false
    \my_functions.php:15:boolean false
    \my_functions.php:15:boolean false
    \my_functions.php:15:boolean false

    It should also be mentioned that this code is in a separate function. I am calling the function from my home page in WordPress. The code works perfectly when it is called from a custom post type page. But it only returns 3 objects on an archive screen before returning null for the rest of the loop.

    I have also tried to fetch the object using the field Key, and while it did return the object, the value attribute was not populated. And that is the part I need.

    It also seems to only return null on Text Fields (After the first correctly populated object). On Select Fields, it runs correctly.

    I hope I have stated things thoroughly enough.

    Thanks for your help Erich

  • This is not code, it is output. Please provide code that is not working

  • Try changing
    $field_value = get_field_object($field[‘name’]);

    To:
    $field_value = get_field_object('my_field');

    Ensure you set my_field to the name of the actual ACF field

  • The code that isnt running correctly is:

    $field_value = get_field_object($field[‘name’]);

  • Thank you for your reply.

    I did try that. Interestingly, when the field in question is of type: Select, it returns an object every time. However, if the field is of type: text, it only returns the object once. Then Null (false) for the rest of the loop.

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

You must be logged in to reply to this topic.