Support Forum
Support › MB Relationships › Trying to update relationshipResolved
I have a new post that is triggered on a profile update. This post adds perfectly. No issues there.
After the post is added, I want to create a relationship to the user. I have tried so many hooks on a 999 priority including transition_post_status and rwmb_after_save_post
The author_Id and new post_ID are got correctly, but the relationship does not set. I am using:
MB_Relationships_API::add($author_id, $post_id, 'users_university_institution');
users_university_institution is the relationship ID from the builder.
What is going wrong?
Thanks!
I can set on an admin_init - so its definitely just not working with the hook. Which hook do you recommend?
Hello,
You can try to use the action hook save_post
or init
and pass specific IDs to the API and check if it works:
MB_Relationships_API::add( 123, 456, 'users_university_institution');
Following the documentation
https://developer.wordpress.org/reference/hooks/save_post/
https://developer.wordpress.org/reference/hooks/init/
Hi on a save_post - no it doesn't work. On an init yes it works, but I don't really want it running on an init. Does it work for you on a save_post? What could be the issue?
All the other fields are able to update on a save_post. Just not the relationships.
Hello,
Hooking the callback function to the save_post
hook, the relationship is added as well. Here is an example
add_action( 'save_post', function() {
MB_Relationships_API::add( 2, 1, 'page-to-user'); //page ID 2, user ID 1
} );
if it doesn't work on your site, you can try to deactivate all plugins except Meta Box, MB extensions plugin and switch to a standard theme of WordPress and recheck the issue.
Ok so I realise it works if I save it twice. Even with plugins it works. But it needs to be saved twice?
What I am doing:
* On user edit - create a new CPT post (on a add_action('profile_update', 'create_university_from_user_data', 10, 2); hook) - this works
* After post is created - add the relationship to the user to the post (still not working)
To clarify, I am facing the same issue with all plugins disabled and triggering directly from Meta Box form - [mb_user_profile_info id="new-institution"]. It creates the post, but does not create the relationship, even though it updates all the other custom fields
Hello Yasmine,
So the issue is related to your custom code that creates posts after submitting the user profile form, then the action hook save_post doesn't work. You can use the init
hook as I suggest above for your case.
Thanks Peter, my save_post
fires correctly - no issues with the hook not working, and the relationship update is executed (just doesn't work).
I only need to fire this occasionally, the init
hook seems a little excessive. But if that's the only option..