Skip Navigation

[Resolved] Write Custom Field with Post Relationships API

This thread is resolved. Here is a description of the problem and solution.

Problem:
Write Custom Field with Post Relationships API

Solution:
You can use the Post relationship API function "toolset_connect_posts" to get the intermediary post ID.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/write-custom-field-with-post-relationships-api/#post-1081042

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This support ticket is created 6 years, 4 months 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by phuong-phiD 6 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1079466
Screen Shot 2018-08-10 at 14.52.02.png

I have defined a custom field for a relationship. Now I want to write the custom field using the post relationship API but I can't find how to do it on the page https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

#1079635

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I would like to know first what is your field type of intermediary post type?

As well as, I would like to know at which point of action you want to update the custom field of intermediary post type? when saving post in backend?

#1080931

Hi Minesh,

- what is your field type of intermediary post type?
text field "source" for the relationship "candidate-event"

- which point of action you want to update the custom field of intermediary post type?
cred_submit_complete

add_action('cred_submit_complete', 'gwf_register_event_new_user', 10, 2);
function gwf_register_event_new_user($post_id, $form_data) {
...
// Connect candidate and event
toolset_connect_posts( 'candidate-event', $candidate_id, $event_id );
// Add custom field "source" to the connection "candidate-event"
$source = $_GET['source'];
???
}
#1081042

Your question about the "intermediary post type" helped me find the solution. A relationship is made with an intermediary post type so I have to write the custom field of the intermediary post type.

To get the post ID of the intermediary post type
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
"On success, also outputs the ID of the created "intermediary_post" (zero is output if none was created)"

To write a field of a custom post type
https://codex.wordpress.org/Function_Reference/update_post_meta
update_post_meta( $post_id, $meta_key, $meta_value, $prev_value );

$connection = toolset_connect_posts( 'candidate-event', $candidate_id, $event_id );

$cpt_id = $connection['intermediary_post'];
$source = 'xyz';
if ($cpt_id != 0) {
  update_post_meta( $cpt_id, 'wpcf-source', $source );	  
}