Support Forum
Support › MB Relationships › relationship doesn't save?
I have defined a reciprocal relationship:
add_action( 'mb_relationships_init', 'your_prefix_function_name' );
function your_prefix_function_name() {
MB_Relationships_API::register( [
'id' => 'registration-partner',
'reciprocal' => true,
'from' => [
'object_type' => 'post',
'post_type' => 'registration',
'empty_message' => 'no partner',
'admin_column' => [
'position' => 'after title',
'title' => 'Partner',
'link' => 'edit',
],
'meta_box' => [
'title' => 'Partner',
'context' => 'normal',
],
'field' => [
'name' => 'Partner',
'label_description' => '= connected registration',
'max_clone' => '1',
],
],
'to' => [
'object_type' => 'post',
'post_type' => 'registration',
'empty_message' => 'no partner',
'field' => [
'name' => 'Partner',
'label_description' => '= connected registration',
'max_clone' => '1',
],
],
] );
}
and inside a function hooked into save_post_registration
I am doing this
$query = new WP_Query($args);
if ($query->have_posts()) {
$partner_id = $query->posts[0]->ID;
error_log("current post: " . $post_id);
error_log("current partner: " . $partner_id);
if (!MB_Relationships_API::has($partner_id, $post_id, 'registration-partner')) {
$relationship = MB_Relationships_API::add($post_id, $partner_id, 'registration-partner', 1, 1);
error_log($relationship);
$relationship = MB_Relationships_API::add($partner_id, $post_id, 'registration-partner', 1, 1);
error_log($relationship);
}
#MB_Relationships_API::add($post_id, $partner_id, 'registration-partner', 1, 1);
# TODO - generalize ID?
if (1) {
rwmb_set_meta($post_id, $this->plugin_prefix . 'partner_registered', 1);
}
}
But the relationship is never saved.
the output of the error.log is:
[06-Oct-2022 17:12:00 Europe/Berlin] current post: 68
[06-Oct-2022 17:12:00 Europe/Berlin] current partner: 143
[06-Oct-2022 17:12:00 Europe/Berlin] 1
which indicates a true
being returned from MB_Relationships_API::add
Hi,
Please share some screenshots when you edit these posts (68 and 143) and check the table wp_mb_relationships
in your database if it has a relationship.
You can also try to add this code to the file functions.php in the theme folder and run it once then check the relation again.
MB_Relationships_API::add( 68, 143, 'registration-partner', $order_from = 1, $order_to = 1 );