Timing of MB_Relationships_API::get_connected() in rwmb_after_save_post

Support MB Relationships Timing of MB_Relationships_API::get_connected() in rwmb_after_save_post

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #49508
    Tanja KropfTanja Kropf
    Participant

    I’m using a bidirectional relationship called special-offers-to-listings, where:

    From: a Special Offer post type

    To: a Business Listing post type

    On the Special Offer edit screen, the user selects a connected Business Listing.

    I’m running some custom logic (sending emails and saving coupon redemptions) immediately after the post is saved.
    However, when I call:

    MB_Relationships_API::get_connected([
    'id' => 'special-offers-to-listings',
    'from' => $post_id,
    ]);

    inside a save_post or even an rwmb_after_save_post hook, it sometimes returns an empty array, even though the relationship clearly exists once I reload the post.

    At what point in the save process are relationships written to the mb_relationships table?
    Is there a specific hook (e.g., rwmb_after_save_field, rwmb_relationships_after_save, etc.) that guarantees MB_Relationships_API::get_connected() will return the correct results right after saving?

    We’re also testing rwmb_meta( 'special-offers-to-listings', ['type'=>'relationship'], $offer_id ) as a fallback — can you confirm whether that retrieves relationship data earlier than MB_Relationships_API::get_connected() during the post save process?

    #49516
    PeterPeter
    Moderator

    Hello,

    Thanks for reaching out.

    I suggest you use the hook mb_relationships_add to ensure the relationship is added between two objects. Regarding the fallback code, the helper function rwmb_meta() doesn't support passing the second argument like this ['type'=>'relationship'], which obviously doesn't work.

    Please read more about the hook in the documentation https://docs.metabox.io/extensions/mb-relationships

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.