Support Forum
Support › MB Relationships › Setting Relationships in Multisite
Hi,
I have the following code which isn't working - I wonder if you could highlight where I'm going wrong (or even if what I'm trying to do isn't possible this way).
switch_to_blog(1);
$post_id = wp_insert_post($new_job);
echo "Post and User IDs - " . $post_id . " - " . $job_data['user_id'] . "\n";
MB_Relationships_API::add( $post_id, $job_data['user_id'], 'jobs_to_clients');
restore_current_blog();
The user and the post are created on one site in a multisite setup and applied to another site. This is working fine. The user is created in the network and added to the other site correctly. The post (CPT) is created in the code above and correctly appears in the other site and both have all their custom meta data working properly.
The only bit that isn't working is the addition of the relationship which I need to have. I don't get an error but the relationship isn't being set.
As a quick update - I've successfully added the row for myself into the mb_relationships table and that works but I feel that this is a bit of a hack and I'd prefer to use the proper helper function if you can tell me what I'm doing wrong with it.
Just wondering if you've had any thoughts on this one as yet. Just looking to tidy up the code if there's a preferred way to do what I'm doing.
Hi Martin,
Was the relationship ID jobs_to_clients
registered on the second site? Or it is just registered on the first site? See more here https://docs.metabox.io/extensions/mb-relationships/#creating-relationships
Hi Long,
The relationship and CPT are both only registered on the second site. I gather details (booking enquiry form) on one site and then create a user and job CPT on the second site from that data. The only part that the first site has is the form to gather the details and I use my own Ajax call from there. I don't register anything on the first site.