Support

Account

Home Forums ACF PRO get_fields() not working if some fields has json data

Solved

get_fields() not working if some fields has json data

  • Hello,
    i’m not sure if this a bug or i’m making it wrong.

    i have some fields like “field_age”, “field_name” and one field “field_json_data” where i store a json string.

    When i try to get all fields

    $sc = get_fields(42);
    var_dump($sc);

    it doesn’t work

    array(1) {
      ["account_type"]=>
      string(0) ""
    }

    when i replace the json string with (for example) a number. get_fields() works and returns all fields from my custom post.

    This works also

    $value = get_field( "field_json_data", 42 );
    echo $value;

    Is this a bug? If not, is it possible to exlude one field (“field_json_data”) from the get_fields() function?

    Thanks in advanced!
    Manu

    EDIT:
    update_field('field_json_data', json_encode($presetPriceList), 42);

  • Fields with JSON in them should work fine.

    Are those the actual field names (starting with field_)? If they are how did you create them?

  • The word field_ was only an example.
    The correct names are json_data, age and name.

    // If they are how did you create them?

    I enter a name in the Field Label and ACF creates automatically the Field Name.

    The Field Type is text.
    The value looks like this:
    {"route_0":{"from":"Sit","to":"Sot","price":"45$"}}

  • Did you mean to mark my question as the answer? Sorry, I was wondering about the field names because field names cannot start with ‘field_’ and if they did that could have been a cause of the issue.

    For JSON values you could use either a text field or a textarea field with not formatting. Or you could use any type of field that will hold text data and when getting the value set the 3rd parameter in get_field($field, $post_id, $format_value) to false to make sure the value is not formatted or altered in any way.

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

You must be logged in to reply to this topic.