Support Forum
Support › MB Relationships › Update an Existing or create New Relationship based upon custom field valueResolved
I would like to be able to set a relationship between two different custom posts types based upon a value stored as a custom field in one of the post types. this stored value is the same value as the post id of an existing post that i wish to relate it to.
In this case, I have 'equipment' and 'clients' that require relating. I will set the correct 'client' post-id in a custom field registered to 'equipment' that is named 'wpcf_related_client_post_id_via_select_field'. When i then update/save the 'equipment' cpt i wish to have the relationship set between the two cpts.
i have a few different use cases for this, firstly to be able to use it as a 'work around' to set the relationship via a csv wp-allimport uplaod, and secondly so that i may be able to create relationships to other custom post types based upon their already existing relationships to the one being updated/created.
ie, i have ttwo other cpts 'job-sheets' and 'engineer-reports' that relate to 'clients' and 'equipments'. My hope is by using this kind of method I will be able to form relationships between all relevant cpts as they are generated in turn.
ie - 'clients', 'equipments', 'job-sheets' and 'engineer-reports' can all relate to one another in various ways and I would like to have the relationships programatically set.
<?php
add_action( 'rwmb_equipment-details-admin_after_save_post', 'update_client_equipment_relationship_from_client_post_id' );
function update_client_equipment_relationship_from_client_post_id ( $post_id){
//get id from related client post id via text field
$related_client_id_value = rwmb_meta( 'wpcf_related_client_post_id_via_select_field', $post_id );
MB_Relationships_API::add( $related_client_id_value , $post_id , 'clients-and-equipment-relationship' );
}
I have tried the above code but i does not seem to create the relationship. please can you share any pointers in the right direction?
Hi,
You should add the second parameter to the helper function, if there are no extra arguments, just leave it blank.
$related_client_id_value = rwmb_meta( 'wpcf_related_client_post_id_via_select_field', '', $post_id );
Read more on the documentation https://docs.metabox.io/functions/rwmb-meta/
Or you can get the client post ID field value via the global $_POST
variable
$related_client_id_value = $_POST['wpcf_related_client_post_id_via_select_field'];
thank you so much, so i have done as you have suggested and it creates the relationship correctly. the final code i used is below;
<?php
add_action( 'save_post', 'update_client_equipment_relationship_from_client_post_id' );
function update_client_equipment_relationship_from_client_post_id ( $post_id){
//get related id from csv import of related client post id
$related_client_id_value = $_POST['wpcf_related_client_post_id_read_only'];
MB_Relationships_API::add( $related_client_id_value , $post_id , 'clients-and-equipment-relationship' , $order_from = 1, $order_to = 1 );
}
the custom field 'wpcf_related_client_post_id_read_only' will be set in advance via csv import. i then plan to use a google web bot to manually/automatically update all the posts for me to set the relationship properly in the custom table. I will then disable the code snippet as it is only to set up the relationships in the first instance, after that it should all be done live on the site, via front end submissions. Out of interest, is/was this a crazy way of achieving this?
Hi,
I've not heard a case about using a Google bot to trigger a PHP/WordPress function on your site to do something. It is also beyond the scope of support of Meta Box so I recommend hiring an expert developer to help you to achieve this goal.