Support

Account

Home Forums General Issues ACF number less than comparison not working

Solving

ACF number less than comparison not working

  • I have two ACF fields named as “price” and “reduced_price” now i have another field named as “price_filter” which should be filled with following conditions,

    – If “price” is greater than “reduced_price” and “reduced_price” is not empty then “price_filter” = “reduced_price”
    – If “price” is 0 or “price” is empty then “price_filter” = 0
    – else “price_filter” = “price”

    Now everything was working fine but when I put “price = 117900” and “reduced_price = 99900” It should fulfill the first condition and display “price_filter” = “reduced_price” but its not working like that and showing “price_filter” = “price”

    Here is my PHP code

    
    function my_acf_save_post( $post_id ) {    
    	$price = get_field('price');
    	$reduced_price = get_field('reduced_price');
    
    	if ($price > $reduced_price && !empty($reduced_price)) {
    		update_field('price_filter', $reduced_price);
    	}
    	else if($price == "0" || empty($price)) {
    		update_field('price_filter', '0');
    	}
    	else {
    		update_field('price_filter', $price);
    	}
    }
    add_action('acf/save_post', 'my_acf_save_post', 20);
    
  • I’m not sure why it isn’t working but you should be able to trouble shoot it easily..

    Are you using a number field or a text field. It would be best to use number and not evaluate your arguments using “string”, as it might return false.

    Print_r(get_field(‘field_name’);

    see what values are being outputted and work from there.

  • When using acf functions inside of an acf/save_post filter you must supply acf functions with the post ID to be updated.

    example:

    
    update_field('price_filter', $reduced_price, $post_id);
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.