Skip Navigation

[Resolved] Save Post A custom field value to Relationship custom fields based on Post B val

This support ticket is created 5 years, 8 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.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 5 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1180191

I have the following situation:
- Custom Post A with custom field x (numeric field)
- Custom Post B with custom field z (select with value "yes", "no")
- M2M relationship between Post A and Post B
- Fields attached to this relationship: field AB-y

What i want to achieve:
- when i initiate a relationship between A and B and the B custom field z="yes" then the A custom field x value to be saved to relationship attached custom field AB-y
- when i edit a relationship between A and B and the B custom field z="yes" then the A custom field x value to overwrite the relationship attached custom field AB-y value
- when i edit a relationship between A and B and the B custom field z="no" then the A custom field x value to not overwrite the relationship attached custom field AB-y value

Please help me to achieve that.

Thank you,

FelixM

#1180199

when i edit a relationship between A and B...
Hi, can you tell me exactly what you mean by editing the relationship? Are you editing other custom fields (other than AB-y), which are attached to the relationship? Are you editing custom fields on one of the related posts? Are you disconnecting post B from post A and instead connecting post B-2? Where are you doing these edits - is it in custom code? Is it in the Post A editor in wp-admin? Is it in a Form?

We have this information available regarding custom PHP code using the post relationships API:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

#1180696

I want to do everything in front-end ....
By "when i edit a relationship between A and B..." i mean the editing of relationship / connection custom fields of Post A with Post B

Here are a scenarios steps:
- first i create Post A and populate all the fields, inclusive custom field x (with value 12 for ex)
- second i create a Post B and complete all the field, inclusive custom field z (selecting value "yes")
- then i start a relationship (connection) from post B - here i use a relationship link - to Post A and when i save that relationship i want that the relationship custom field AB-y to be populated automatically with the value of custom field A x (with value 12 as i exemplified above)
- when i edit a relationship (connection) starting from post B - here i use and edit relationship link - and save it agan i want that the relationship custom field AB-y to be overwritten with value x of Post A if the custom field z of post B has value "yes" or not to be overwritten if custom field z of post B has value "no"
-------

I try to transpose my needs on the toolset documentation sample with many to many relationship between "Tracks" (post A in my example) and "Album" (post B in my example). Supplementary than in toolset example we have the following custom fields:
- Track has a custom field "track length" with numeric value (custom field x in my exampe)
- Album has a custom field "Launched" with select type value "yes/no" (custom field z in my example)
- on connection custom fields between Track and Album i have beside "Track number" a new custom field called "Track length on album" with numeric value (this is AB-y in my example) - [Note: this field will not be editable by nobody in front-end"]
So :
- when i initiate a connection (relationship) between Albums and Track (using button "add track to album") I want that field value of "Track length" to be saved as value for "Track length on album"
- when i edit the connection (relationship) between Album and Track (using button "edit this connection(Album Track)") I want that value of connection custom field "Track length on album" to be overwritten by value of "Track length" if custom field "Launched" has value "no"
- when i edit the connection (relationship) between Album and Track (using button "edit this connection (Album Track)") I want that value of connection custom field "Track length on album" NOT to be overwritten by value of "Track length" if custom field "Launched" has value "yes" *

*Note: lets say for example that in time i change the value for "Track length" of a Track and i will edit a connection between Album and Track and if the Album is Launched (value of field "Launched" equal "yes" i don't want that the value of connection field "Track length on album" to be changed, i want to keep the old value of "Track length" that was set when Album has the "Launched" value set to "no"

I hope this help you to understand me better

Thank you

#1180894

Hi, I understand better now. Unfortunately there is no API available for Relationship Form events. Our current Forms API provides some hooks like cred_save_data that you can use to perform custom field manipulation, but that hook doesn't fire when submitting an Edit Relationship Form. It only works on post and User forms. So the only way to programmatically set custom fields after Form submission is to recreate this Relationship Form as a standard post editing Form, where you can edit the intermediary post type. This means your intermediary post type must be visible in wp-admin. You can remove the parent post selection fields from the Form contents. You must also create a Content Template (or Template Layout, if your site uses Layouts) and insert your new Edit Intermediary Post Form. Then instead of using an Edit Relationship link in a View of related posts, insert an Edit Post link in a View of intermediary posts to link to your Edit Intermediary Post Form. Then you can use the cred_save_data hook and the post relationships API to get and set custom field values based on your own logic.

Here is an example of the cred_save_data hook and updating a custom field in an intermediary post using the value of a custom field in the parent post:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
  $forms = array( 12345 );
  if ( in_array( $form_data['id'], $forms ) )
  {
  // modify the custom field 'color'
    $parent = toolset_get_related_posts(
    $post_id, // get posts related to this one
    'relationship-slug', // relationship between the posts
    'intermediary', // get posts where $writer is the parent in given relationship
    1, 0, // pagination
    array( ),
    'post_id'
    );
    $parent_color = get_post_meta( $parent[0], 'wpcf-parent-color', true);
    update_post_meta($post_id, 'wpcf-color', $parent_color );
  }
}
This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.