Custom Fields
Re-Envisioned
Support MB Relationships Connected "From/To" relationships obtained via API are backwards (?)

  • This topic has 6 replies, 2 voices, and was last updated 1 month ago by bzx.
  • Creator
    Topic
  • #32555
    bzx
    Participant

    Hello,

    I've got a local WP installation, with MB plugins installed (Metabox 5.4.8, Relationships 1.10.9). I was working on a single query to get a list of posts along with their connected posts, since my initial try was having a loop with MB-related query inside. I've found in the docs that the way to do it is via MB_Relationships_API::get_connected and it was actually quite simple to achieve.

    I've noticed however that the relationships queried this way seem to be backwards...

    I've got 3 posts, and the first (Post One) is connected TO the other two via WP admin, as shown below:
    WP admin

    I'm querying the relationships with the "from" keyword in the query arguments:

    'connected_from' => MB_Relationships_API::get_connected([
        'id'    => 'posts_to_posts',
        'from'  => $post->ID
    ])

    ...and in the result, I was expecting Post Two and Post Three to show the relationships FROM Post One.
    Instead, Post One has the relationships FROM the other two posts (dotted out irrelevant fields from the result):

    [connected_from] => Array
    (
        [0] => WP_Post Object
            (
                [ID] => 149 
                ...
                [post_title] => Post Two
                ...
                [mbr_posts_to_posts_from] => 145
            )
    
        [1] => WP_Post Object
            (
                [ID] => 152 
                ...
                [post_title] => Post Three
                ...
                [mbr_posts_to_posts_from] => 145
            )
    )

    (full result dump here: pastebin)

    As expected, if I use "to" keyword in the arguments of the query, then I'm getting it the other way around: Post Two and Post Three are connected TO Post One.

    Is this a bug or it's working as designed?
    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
    Replies
  • #32562
    Long Nguyen
    Moderator

    Hi,

    Yes, it works as designed. While the relationships are registered clearly with the term from and to, the connections are bi-directional. You will be able to query back and forth without any problem.

    Please read more on the documentation
    https://docs.metabox.io/extensions/mb-relationships/#bi-directional-relationships
    https://docs.metabox.io/extensions/mb-relationships/#posts

    #32568
    bzx
    Participant

    Thanks for your response!

    I understand that connections are bi-directional, but the API requires me to explicitly specify the keyword (relationship direction) in the arguments (from or to) – therefore I would expect the query to execute accordingly. Right now it is backwards, don't you think it's quite unintuitive?

    Simple example: Authors connected TO any Books they wrote. On the page showing Authors if I used to keyword in the query, it wouldn't show me any connections...

    #32574
    Long Nguyen
    Moderator

    Hi,

    I don't think it's quite unintuitive. You can try to create a relation between posts and pages (or a CPT) to understand how it works.

    Regarding your example: What is the relationship between Authors and Books? If the author is the user who created the books (default of WordPress), it's not a relation created by Meta Box and won't show the connections on the frontend.

    #32575
    bzx
    Participant

    In my example, my requirement is that both Authors and Books are posts (not the built in WP author role) – so in order to create a relation between them I am using MB Relationships – and I am connecting an Author TO the Book. If then I want to have a page showing all Authors and display their Books, I am using the get_connected() method from the API, but I need to specify the from keyword in the query arguments... You're right, it is not unintuitive – it's illogical 😉

    #32595
    Long Nguyen
    Moderator

    Hi,

    Using the term keyword from or to is a good way to separate the objects connected. If you have more post types that have relationships to the Author or Author itself, what will you do to query to get a specific object type? You might have all object types that connect from and to the Author.

    #32622
    bzx
    Participant

    To answer your last question – I digged a bit more into the documentation & forum posts, and the solution would be to register another relationship, and create relations (reciprocal to simplify this) in the WP Admin..

    Actually, you asked this right in time, since I realised I would need this! 🙂 I need to query all relationships between posts, which are from various categories, and I couldn't find any way to filter, as there is no category field returned from the API... unless there is?

    Thanks

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