Connected "From/To" relationships obtained via API are backwards (?)
Support › MB Relationships › Connected "From/To" relationships obtained via API are backwards (?)
- This topic has 6 replies, 2 voices, and was last updated 3 years, 4 months ago by
bzx.
-
AuthorPosts
-
December 12, 2021 at 8:12 PM #32555
bzx
ParticipantHello,
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:
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!December 13, 2021 at 10:08 AM #32562Long Nguyen
ModeratorHi,
Yes, it works as designed. While the relationships are registered clearly with the term
from
andto
, 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/#postsDecember 13, 2021 at 5:17 PM #32568bzx
ParticipantThanks 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
orto
) – 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...December 13, 2021 at 10:25 PM #32574Long Nguyen
ModeratorHi,
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.
December 13, 2021 at 10:31 PM #32575bzx
ParticipantIn 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 thefrom
keyword in the query arguments... You're right, it is not unintuitive – it's illogical 😉December 14, 2021 at 12:46 PM #32595Long Nguyen
ModeratorHi,
Using the term keyword
from
orto
is a good way to separate the objects connected. If you have more post types that have relationshipsto
the Author or Author itself, what will you do to query to get a specific object type? You might have all object types that connectfrom
andto
the Author.December 15, 2021 at 3:04 AM #32622bzx
ParticipantTo 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
-
AuthorPosts
- You must be logged in to reply to this topic.