Skip Navigation

[Resolved] Setting the Relationship from form

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 7 replies, has 2 voices.

Last updated by Beda 6 years, 1 month ago.

Assisted by: Beda.

Author
Posts
#1126371

I am using a Gravity Form to submit to a Custom Post Type. In Toolset > Custom Fields, I have a post-relationship field. Everything works except setting the field for post-relationship. I'm having the Gravity Form submit the related post's ID to the post-relationship field. This works, sort of.

I know that the post-relationship field is using the post ID. From within a post I can output the post-relationship field and it's showing the ID. But when I go to edit the post, the post-relationship field shows blank.

Is there a special way that I need to submit data to that field?

#1126820

Do you mean the "Post Reference" Field as documented here?
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/using-post-reference-fields-to-display-information-from-a-related-post/

That field is a Select2 Field, in the database, it will be stored as a native WordPress PostMeta, with meta_key "wpcf-your_field_slug_here", with a meta_value corresponding to the ID of the post you chose to be referenced (as you correctly recognized).

Now, I understand you populate that field with another Plugin (Gravity Forms).
However, I am not entirely sure how you populate the field itself in the Gravity Forms

I cannot add that field in Gravity Forms, I can only add native Gravity Forms fields in those forms.

Could you show me how to set this up so we can replicate it?

#1126824

Try this: hidden link

Use the Post ID for the value:

$choices[] = array( 'text' => $post->post_title, 'value' => $post->ID );

Takes the submission but doesn't show when you view the CPT field.

#1126828

That code did nothing, I tried it on a Gravity form with the select field (adjusted the ID of the code as mentioned in the DOC)
Please be more specific in the steps we need to perform, if the issue is in a/with a 3rd Party Software.

However, it does not matter in this case, I found that a simple update_post_meta() on that field also does not work, it does properly populate the Database, but not the Select2 Field on the Post Edit Screen, which is what you report (just, using Codex of WordPress)
So the issue is on the Select2 Field that expects something else, I can however not spot what it is just yet.

I escalated this ticket so we can find why this does not work.

Technically, the information should be there, means you should be able to work/retrieve the details (display) but you cannot see them saved on the very post screen, we will try to fix that ASAP.
I assume this will then also solve the issue with Gravity Forms, I think.

#1126829

change state

#1128469

Hmm... not sure what change state means. But it's just not working.

#1128884

"Change State" I posted because I had to change the state of this ticket to "Escalated to Second Tier".
My apologies, I needed to make a comment otherwise the system won't let me escalate the ticket and I should have been more clear on my action.

It does not work, correct, as I confirm here:
https://toolset.com/forums/topic/setting-the-relationship-from-form/#post-1126828

The solution meanwhile found is that we do store not only a Post meta but also a relationship, which we would need to do manually here using the API:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
We need to set probably both post meta and that relationship.

I am working on a solution for this for you to apply which I will communicate here today.

The solution consists in update_post_meta() AND toolset_connect_posts()

#1129005

This is the solution:

toolset_connect_posts( 'prf_slug', $post_id_of_post_to_relate_to, $post_id_of_post_where_prf_is_saved );

prf_slug is the slug of your Post reference field.
$post_id_of_post_to_relate_to and $post_id_of_post_where_prf_is_saved are integer (numeric ID of posts)

That will properly populate the Select field of the PRF (Post Relationship Field) however it will not update the post meta, that, you still run with update_post_meta.

So that means, to populate Post Relationship Fields properly, you need to run 2 functions at least:
toolset_connect_posts and update_post_meta.
If your Plugin which updates this field does not offer methods to run such functions, you would have to populate it elsehow.
If they offer a hook where you can fire your own code, you can update the field with above mentioned API.