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?
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?
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.
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.
Hmm... not sure what change state means. But it's just not working.
"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()
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.