Trying to update relationship
Support › MB Relationships › Trying to update relationshipResolved
- This topic has 8 replies, 2 voices, and was last updated 9 months, 2 weeks ago by
Yasmine.
-
AuthorPosts
-
June 28, 2024 at 5:57 PM #45759
Yasmine
ParticipantI 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!
June 28, 2024 at 9:53 PM #45760Yasmine
ParticipantI can set on an admin_init - so its definitely just not working with the hook. Which hook do you recommend?
June 30, 2024 at 1:23 PM #45777Peter
ModeratorHello,
You can try to use the action hook
save_post
orinit
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/June 30, 2024 at 8:28 PM #45785Yasmine
ParticipantHi 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.
July 1, 2024 at 9:04 PM #45793Peter
ModeratorHello,
Hooking the callback function to the
save_post
hook, the relationship is added as well. Here is an exampleadd_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.
July 1, 2024 at 10:51 PM #45800Yasmine
ParticipantOk 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)July 1, 2024 at 10:53 PM #45801Yasmine
ParticipantTo 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
July 5, 2024 at 9:32 PM #45837Peter
ModeratorHello 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.July 9, 2024 at 3:27 PM #45856Yasmine
ParticipantThanks 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.. -
AuthorPosts
- You must be logged in to reply to this topic.