Support

Account

Home Forums General Issues get_fields() – returns number fields as "2" instead of 2

Solved

get_fields() – returns number fields as "2" instead of 2

  • Hello, I have a field that I have set as a “number” inside of ACF. When I get the data using the get_fields() it returns the number fields as string numbers, not int.

    Example:
    token_life:”3″
    first_query_quantity:”30″
    next_query_quantity:”20″

    Should be:
    token_life:3
    first_query_quantity:30
    next_query_quantity:20

  • Yes, ACF returns number fields as strings.

    Add a filter in functions.php

    
    add_filter('acf/format_value/type=number', 'acf_number_str_to_number', 20, 3);
    function acf_number_str_to_number($value, $post_id, $field) {
      if (empty($value)) {
        return $value;
      }
      $int = (int)$value;
      $float = (float)$value;
      if ($int == $float) {
        return $int;
      } else {
        return $float;
      }
    }
    

    You’ll need to contact the developers to change it any other way.

  • Thank you for your reply this is just what I needed!

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

You must be logged in to reply to this topic.