• Resolved ds7ilg7

    (@ds7ilg7)


    Hello Konrad,

    I read the topic of the attached link because I’m trying to do the same as the user. However, I think you may have misunderstood his question. (…or I may have.)

    I have a Customer CPT. The customer creates their account through a previously filled form. Once the account is created, the customer can access the front end of their account where they can complete an order form.

    The Order is a separate CPT (each performs functions that are sometimes unrelated). The Customer and the Order are joined by a bidirectional relationship field.

    When a customer is in their account and when they fill the order form, a new order is created in the Order post type.

    I need to assign the Order with the Customer. I had the same idea as kamyarhu (the user who posted the original question) I created a hidden field with the same name as the bidirectional field and during post save, the field saves the current post id in the new Order post in the identically named relationship field. In my theme functions I passed the current post id to the form field and it works – but for me, only on the Order side. The relationship field isn’t reciprocated on the Customer side – even though it’s a bidirectional field.

    I read your answer and tried your suggestion of creating a relationship field hidden on the front end. My issue is that by the time the new post is created, I no longer have the customer post id where the order form was created, so I don’t know how to update the bidirectional field.

    It seems like it would be fairly common to associate a new post with the post where the form resides… Do you have any suggestions?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback! I’ve read the topic you linked, and my answer seems alright, but the original author didn’t provide much details about his configuration so I had to guess some key points. The update_field() code example I provided should use the current post id as value tho. Unfortunately I can’t update it since it’s an old thread.

    Your question is kinda similar, so the solution will be the same. Unfortunately using a Hidden Field with the same name as the bidirectional field will not work, because ACF Extended use the field configuration, and not “field names”, to set a birdirectional relation.

    This is why the update has to come from the actual field that has been configured as bidirectional. Therefore you should use update_field() function to save it within the acfe/form/submit/post hook (when the order is generated). See documentation. Here is a usage example:

    add_action('acfe/form/submit/post/form=my-form', 'my_form_submit', 10, 5);
    function my_form_submit($new_order_id, $type, $args, $form, $action){
        
        // Get the page id where the form is displayed (Customer CPT Post ID)
        $customer_id = get_the_ID();
    
        // Update the order bidirectional field
        update_field('my_relationship', $customer_id, $new_order_id);
        
    }
    

    If you can’t retrieve the Customer CPT Post ID using get_the_ID() (if you display the form somewhere else than Customer CPT Single template for example), then you can pass that custom value during the form render:

    acfe_form(array(
        'name'        => 'my-form',
        'customer_id' => 122 // Pass customer ID here
    ));
    

    You’ll then be able to retrieve that value in the same acfe/form/submit/post hook using $form['customer_id']. See documentation.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter ds7ilg7

    (@ds7ilg7)

    THANK YOU SO MUCH KONRAD!

    I worked and re-worked my method(s) for 2 days with no success. It took me about 3 minutes to accomplish what I needed by following your solution.

    Since working with ACFE, I’ve been able to figure many things out by stalking your support forum for solutions to other problems. It’s obvious that you spend a lot of time understanding your user’s questions and you are extremely helpful with your detailed replies.

    Again, thank you.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    I’m glad to hear it now works as expected 🙂

    Yes I try to deliver the highest support quality, as fast as possible. It require more time/concentration to explain all the logic, but I think it always work better when the user understand the process behind each solutions.

    Thank you very much for the kind review BTW!

    Have a nice day!

    Regards.

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

The topic ‘Relationship to current post id’ is closed to new replies.