Support

Account

Home Forums General Issues get_field() and get_fields() returns: array(field_key => false)

Solved

get_field() and get_fields() returns: array(field_key => false)

  • Straight to the point:

    Here’s what i get when i run get_post_meta():

    // Removed unrelated meta fields such as "_edit_last"
    
    array(6) {
      ["mail_header_title"]=>
      array(1) {
        [0]=>
        string(30) "...data removed..."
      }
      ["_mail_header_title"]=>
      array(1) {
        [0]=>
        string(19) "field_51ee1db3aa7a0"
      }
      ["mail_header_description"]=>
      array(1) {
        [0]=>
        string(18) "...data removed..."
      }
      ["_mail_header_description"]=>
      array(1) {
        [0]=>
        string(19) "field_51ee1f63241e5"
      }
    }

    Here’s what i get when i run get_fields():

    array(2) {
      ["field_51ee1db3aa7a0"]=>
      bool(false)
      ["field_51ee1f63241e5"]=>
      bool(false)
    }

    What i want it to return (and what it *should* return according to the doc):

    // Pseudocode, ofc
    array(
      'field_name' => 'field value'
    )

    I have compared the field keys through admin and they are indeed correct.

    I found this in the documentation for get_fields():

    This is a very simple function which finds ALL custom field values **(that don’t start with an “_”)**…

    But i have both with and without _ stored. I assume that’s normal, but i don’t understand why it returns the wrong data. It works just fine everywhere else, it’s just for this specific field group it messes up.

    Any ideas?

  • Hi @nike.sandell@gmail.com

    Perhaps you need to pass through the $post_id parameter to the get_fields function.

    Looks like totaly different data is being returned. This may be due to ACF loading from the wrong post_id

  • Hi, @elliot

    I was passing the $post_id to both functions, i even tried manually passing it.

    I solved it though! The problem was that i was calling the functions before ACF has initiated completely (i’m guessing). I put the code inside the init action and now it runs beautifully.

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

The topic ‘get_field() and get_fields() returns: array(field_key => false)’ is closed to new replies.

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.