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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.