Home Forums General Issues Mathematical operation in ACF field


Mathematical operation in ACF field

  • Hello,

    i want to do mathematical operation in acf fields, but i can’t make it work.
    I have 3 fields: “eye”, “movement”, “verbal”
    All contain number value generated from radio button fields: 4, 6, 4

    i create this code for that fields :

    $x1 = the_field('eye');
    $x2 =  the_field('movement');
    $x3 =  the_field('verbal'); 
    $x4 = $x1+$x2+$x3 ;
    echo  "</br> Value is $x4" ; 

    And the result in front end is :

    Value is 0

    The result should be 14, but it keep displaying 0
    And i don’t know why the value 464 is displaying in front end, it should be not displayed.

    I hope somebody can help, thanks before.

  • Use ‘get_field();’

    $x1 = get_field('eye');
    $x2 =  get_field('movement');
    $x3 =  get_field('verbal'); 
    $x4 = $x1+$x2+$x3 ;
    echo  "</br> Value is $x4" ; 
  • with get_field() function,
    the frontend generate :

    Value is Array

    still not working.

  • Are you still having trouble with this? If so, could you post a screenshot of your ACF settings for those fields, or alternatively the output of var_dump() for each $x1, $x2, and $x3 ?

  • Yes, i’m still having trouble with this.

    Ok, here is the var_dump result :

    with the_field()

    Result the_field()

    with get_field()

    Result get_field()

    and this the acf setting for that fields :


    I hope you can help.
    Thanks before.

  • The problem is that you use checkboxes and not radio buttons. You can choose multiple values with checkboxes, so your selection will be saved in an array. If you just want to select one item you have to use radio buttons. And then the output will be a string instead of an array.

  • Many thanks, it works.
    I didn’t noticed about checkbox.

    Thanks Philipp..

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

The topic ‘Mathematical operation in ACF field’ is closed to new replies.