Support

Account

Home Forums ACF PRO post_object format value hook crash if used inside repeater

Solving

post_object format value hook crash if used inside repeater

  • Hello! I’m using the following code to add fields to returned post objets.
    It works perfectly fine, however if i add a post_object field inside a repeater, it causes internal server errors 🙁

    function my_acf_format_value( $value, $post_id, $field ) {
        if (is_array($value)) {
            foreach ($value as $v) {
                if (get_fields($v)) {
                    $v -> acf = get_fields($v);
                }
                if (get_the_category($v)) {
                    $v -> category = get_the_category($v);
                }
                if (get_permalink($v)) {
                 $v -> link = get_permalink($v);
                }
            }
        }    
        else {
            if (get_fields($value)) {
                $value -> acf = get_fields($value);
            }
            if (get_the_category($value)) {
                $value -> category = get_the_category($value);
            }
            if (get_permalink($value)) {
              $value -> link = get_permalink($value);
            }
        }
        return $value;
    }
    // need priority > 10 else type=post_object causes errors
    add_filter('acf/format_value/type=post_object', 'my_acf_format_value', 100, 3);

    Any idea what needs to be done for this to work inside repeaters?

    Cheers

  • Calling get_fields() is likely creating an infinite loop.

    Add this at the top of your function

    
    remove_filter('acf/format_value/type=post_object', 'my_acf_format_value', 100);
    

    and then add this before your return

    
    add_filter('acf/format_value/type=post_object', 'my_acf_format_value', 100, 3);
    
  • Unfortunately this is still causing internal server errors 🙁

    Tried commenting out the foreach loop, still crashes
    Even simply trying to add fields for get_permalink or get_category causes it to crash

    It does not crash when i add string fields

    $v -> ‘test’ works

    Im using wordpress as rest api so im not sure how to get details about the error

    Wp debug mode set to true but doesnt record any error logs 😐

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

You must be logged in to reply to this topic.