Home › Forums › General Issues › Using Post Object Relationship field with add_post_meta in front end form
Hey, I’m building a front end form for users to submit guest reviews on apartments.
In my ‘review’ post type, I have an ‘apartment’ post object field.
On my single-apartment.php template, I have a front end form to collect guest reviews for that particular apartment.
Within this form I am passing through most fields without any problems. However I am not sure how to pass through data to a post object field. Passing through the post ID doesn’t seem to work. (see highlighted on review-frontend-code.jpg – 999 is this particular apartment ID)
When the form is submitted, and the new post within my ‘review’ post type has been created, I want the ‘apartment’ field to have the relevant apartment selected.
So how can I pass through data to the post object field from the form?
Appreciate the help.
Most of your fields are basic field types and post object fields are not.
You are inserting the correct value, and in the admin ACF should be picking this field up correctly and showing the correct apartment, I’m not sure why it’s not. One possibility is that the post ID does not meed the criteria set for the post object field, for example it’s the wrong post type. Another possibility is that the post object field is set to allow multiple selections, if it is then ACF could be looking for an array (serialized) in the db value. How is the post object field set up?
Hey John, thanks for letting me know it should be working.
I have now resolved the issue, it seems a custom permalink plugin was interfering with the ‘apartment’ field. I changed the field name and it’s working.
Just a word of warning, the post object field will not be picked up correctly if used on the front end of the site unless you edit/save the post in the admin… well depending on what you’re returning. If you’re returning the post ID then you’ll be fine but if you try to get the post object then it will still only return the post ID. In order to get ACF to return the post object on the front end you also need to save the field key reference to the database. That would look something like this
add_post_meta($post, '_apartment', 'field_012345', true);
Hi there, how are you?
How did u insert the relationship field inside ACF fields?
like a normal text field?
You must be logged in to reply to this topic.
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!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.