Support

Account

Home Forums ACF PRO Get the $post_id in render_field method Reply To: Get the $post_id in render_field method

  • A reliable hack that I used was to hook into ‘acf/pre_render_fields’ filter which allows you to modify the fields before they are rendered. There you can attach the $post_id to the field itself, then when it comes to rendering it, you can access it on the field.

    Crude example

    
    add_filter('acf/pre_render_fields', function($fields, $post_id) {
      return array_map(function($field) use ($post_id) {
        $field['my_post_id'] = $post_id;
        return $field;
      ), $fields};
    }, 10, 2);
    
    add_action('acf/render_field', function($field) {
      echo $field['my_post_id'];
    });
    

    Yuk, I know! You’ll have to make the function recursive if you want to get to sub fields.