Support

Account

Home Forums General Issues get_field returns array, field type is text.

Solving

get_field returns array, field type is text.

  • I am using the ACF plugin to update some fields and repeater fields when the user completes a form.

    The only way I could get the repeater fields and some other fields to update was to use the field key.

    Everything is working great until I try and retrieve these fields. When I try and retrieve a flat text field it returns string(5) “Array”

    If I go into the backend of WordPress and update the post type it will then return the correct value, but I do not wish to update 100’s of individual posts, any ideas on what is happening here?

    The process is:
    User submits a form:
    Create a post > Get the post id > update custom fields.

    I display all the submitted forms on frontend:
    Loop through posts > get custom fields.

    I am setting the fields like this:
    update_field('custom_field_one', filter_var($_POST['custom_field_one'], FILTER_SANITIZE_STRING), $post_id);

    I am getting the fields like this:
    $field = get_field('custom_field_one', $post->ID);

    Any help would be greatly appreciated!

  • I’m having the same issue. I have an option on a link, a checkbox to say, “Open in new window”. When it’s checked, I use:

    $new = get_field("new_window");

    to return whether this is set or not. If I do:

    if(in_array('set', $new))

    I get an error telling me second variable in in_array must be array

    If I do

    if(strpos($new, 'set') !== false)

    I get an error telling me first variable must be a string. PHP eh?!

    For whatever reason, I can’t do:

    if($new == 'set')

    as this always returns false. A var_dump of the $new variable shows it is indeed an array.

    Any ideas?

    Edited: Actually, I should clarify, these are PHP notices, not errors. I’m not too worried, but I would like to resolve. If the answer is “Because, PHP” well then, that’s it…

  • Hi @robmehew

    Could you try using WordPress sanitize_text_field() function to do the sanitation just to make sure there’s nothing outside of ACF giving you this “error”. https://codex.wordpress.org/Function_Reference/sanitize_text_field

    @tadywankenobi that is weird indeed. So the variable $new is definitely an array all the time? You haven’t declared $new somewhere else as well or use the same field_name for a different meta field on the same post? And the value you’ve set for the checkbox is “set”?

    Also, you can try instead of a checkbox field to use a true/false field since that’s pretty much what you want.. It’ll return either true or false (1/0 .. duh ๐Ÿ˜‰ ).

  • Yup, positive, it’s a new variable I’ve created.

    I’ll try the true/false idea next time I use one, see if it resolves the issue.

    I’m baffled by it, but like I say, the var_dump does indeed show the checkbox content as an array.

    Cheers

  • I have the same issue.

    I have a text field that contains a year, in this case 2015. On my computer it works fine, but when I deploy it, the field says “Array”…

    When debugging on the staging server, where I just deployed, it outputs [“2015”, “2015”]… I have no idea why it’s double.

    I tried to delete the page, and import it again, and it said that the post already exists. So maybe that could be why… but the post didn’t exists after I deleted it… I’m stuck… ๐Ÿ™‚

  • Okay, so I figured out that it must have something to do with the import/export. When I delete it, and enter it myself, without importing it, then it works just fine. But it would be nice to know how to use the import.

  • @kennethbl so when you imported the ACF field group containing a text field the value of that field instead came out as an array..?
    Did you have any previous meta fields in the site you imported to that might’ve had the same field name? (saves to same name in wp_postmeta).

    Did you check the imported field group to see that everything looked fine?

  • I got an array being returned from an ACF field after moving onto my production web server. The cause is, I suspect, the bad migration procedure. I re-imported several times the WP database via XML export files one on top of another and again.

    As I use Timber/Twig, my code causing errors is {{post.front_page_quick_order|apply_filters('the_content')}} where the ACF field is front_page_quick_order.

    Making var_dump I saw clearly that front_page_quick_order was an array of strings.

    Searching in the WP database via PHPMyAdmin for front_page_quick_order gave me 25 entries in the wp_postmeta table.

    The quick fix 1 was: {{post.get_field('front_page_quick_order')|apply_filters('the_content')}} – just use the standard ACF function to get the field instead of Timber/Twig shorthand syntax.

    The fix 2 (the one I prefer): install WordPress WP-Sweep plugin and clean up the entire DB. Then return the code to the initial state {{post.front_page_quick_order|apply_filters('the_content')}}.
    NB: I tried WPOptimizer with no effect. After WP-Sweep 25 entries become 3.

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

You must be logged in to reply to this topic.