Home Forums ACF PRO Get the $post_id in render_field method


Get the $post_id in render_field method

  • When creating a custom field type, the $post_id value is passed to other methods such as “update_value” and “format_value”, but is not passed to the “render_field” method. Is there an easy/reliable “ACF way” to get the current $post_id (whether its a post, term, user, option, etc) for this method, or in the admin in general?

  • Hi @emrl

    I’m afraid you need to create your own function to get them. The easiest way would be using the $_GET variable to get the data you want. For example, you can get the post ID like this: $_GET['post'], the term ID like this: $_GET['tag_ID'], the user ID like this: $_GET['user_id'], etc.

    For further support, please ask it at WordPress support forum.

    Thanks 🙂

  • @acf-support

    Sorry, this isn’t solved anymore.

    I noticed that $post_id is passed to acf_render_fields method, but then it doesn’t get passed to acf_render_field_wrap or acf_render_field. It really should be passed along, so that metadata can be loaded when displaying the input field.

  • Hi @emrl

    The ID is not passed to those functions because they don’t need it to do their job. Adding it would be redundant and can create confusion in the future. Was it impossible to get the ID from the $_GET variable?

    If you think that the ID needs to be passed to those function, you can always open a new ticket so your request can be passed directly to the plugin author. You can open a new ticket here:

    Thank you very much 🙂

  • @acf-support

    Who says that they don’t need it to do their job?

    Here’s a very basic use case: a Markdown field. After saving the field, the converted value is saved to the database in a separate field (using acf_update_metadata($post_id, $name, $value, $hidden)). That’s done so we’re not saving the original value as an array, allowing easy querying via WP_Query or the like.

    When rendering the input field next time, we show a preview of the converted markdown field, but we need the $post_id to get the correct field from the database (acf_get_metadata($post_id, $name, $hidden)).

    I will open a support ticket, thank you.

  • @acf-support

    To answer your question about it being impossible to use $_GET to get the IDs, it is not hard to do for some things like posts and terms, but for widgets it is hard, because there is no $_GET variable available (when JavaScript is enabled). You would basically have to add a filter to every widget that sets a global variable or something similar. But since ACF is already doing all the work, why not share it?

  • @emrl

    Did you manage to get this to work? I’ve run out of ideas on how to access the post ID.

    I’m loading a front-end ACF form via AJAX, and need the post ID because I have a custom ACF field I’ve created that loads the child posts of the loaded post.

    Any pointers would be great!

  • @emrl

    Oops! Don’t worry about it, I just realised I can access the post ID from my previous AJAX call via $_POST.


    I still agree with @emrl and think it’d be good to include the post ID somehow. As an example, my application is a task manager. When clicking on a task a modal pop-up opens with the acf_form. I have registered a custom ACF field that lets you add subtasks, so when adding one I’m calling wp_insert_post() and I need the task post ID to set the post_parent on the subtask.

    I’m sure there are many more scenarios where the post ID would be useful 🙂

    Cheers guys, keep up the good work!

  • 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.

  • Calling this inside the render_field hook works for me:


  • @enaldi that only works if you’re editing a post. Won’t work for options/users/terms.

  • If it’s possible to add $post_id to render_field, why doesn’t ACF just do it? It might not be used all the time, but having access to the post ID is always a good idea.

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

The topic ‘Get the $post_id in render_field method’ is closed to new replies.