Support » Plugin: Advanced Custom Fields: Extended » Form submission: set relationship field to current post ID

  • Resolved kamyarhu


    I have a relationship field in my cpt. I am using dynamic forms to display a form in front page, to create new posts. Is there a way to keep the relationship hidden, and also to set it to the current post ID?

    I have tried advanced conditions to hide it, or to set the default value to {current:post:ID} without luck. Hiding it will actually remove it from the form.

    By the way, your plugin is like an art form, beautiful, functional, and with vast possibilities which can be made more visible with documentation. Thank you so much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter kamyarhu


    This worked, but I am not sure if this is the best way.

    function my_prepare_field( $field ) {
    if ( !empty($field ['value']) ){
        return $field;
    else {
        $eventid = get_the_ID();
        $field ['value']= $eventid;
        return $field;
    add_filter('acf/prepare_field/key=field_5ef201d35ce09', 'my_prepare_field');
    • This reply was modified 1 year, 7 months ago by kamyarhu.
    Plugin Author hwk-fr



    Thanks for the feeback! So there’s different points in your report:

    • If you use the “Hide field” setting, the field won’t be displayed at all, which means it won’t be saved
    • Yes, there are multiple ways to achieve what you want you want to do
    • There is no such template tag as {current:post:ID} that can be used as default value for ACF fields in the ACF Extended: Forms UI

    So it looks like you have a form that create a New Post. This form is displayed on the front-page. You probably want to set the “Relationship” value of the newly created post (and not the front-page ID), right?

    The code you provided will work, but not as intended. This code will be executed in the front-end and the back-end, so be careful. The get_the_ID() you’re using will retrieve the current post ID, when in the front-end, it will set the value as the front-page ID. Then in the back-end, it will be overwritten and set the value as the current back-end post.

    This is weird behavior, and you should avoid it. Here is a way to achieve what you want to do:

    You can keep you relationship field hidden in the front-end, using “Hide Field” setting. When your post is created, you can add a specific action that will be executed after the post creation and set the newly created post ID into your ACF field. Please see the “Advanced” tab of the “Post Action” for more code examples:

     * Do an action on post creation in the form named 'my-form'
     * Please change the 'my-form' part of the hook name
    add_action('acfe/form/submit/post/form=my-form', 'my_form_post_save', 10, 5);
    function my_form_post_save($post_id, $type, $args, $form, $action){
        // Add the value of the newly created post in the 'my_relationship' field
        update_field('my_relationship', $post_id, $post_id);

    Hope it helps!


Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Form submission: set relationship field to current post ID’ is closed to new replies.