Hi there, we have a relationship set for custom post types. "Owners" are associated with "Artworks" in a "Former Owners " Relationship. This is so that we can display historical owners of a given artwork piece. We also have a "Current Owner" relationship with "Artworks" & a single "Owner" which is easy to query within a single view (loop).
We're having issues on querying the "Former Owners" relationship to display ALL of the "Former Owners" established in the relationship. The confusion is that a many to many relationship is set for this relationship. And so we're not sure if a parent/child relationship exists - as I don't think that relationship is necessarily a hierarchy.
How would we accomplish displaying a view that will query this relationship so that we can embed that in a parent loop that will for example display all types of artwork on a given page?
The artworks post types would display on this page, and with each artwork within the parent loop, we'd like to list all of the "Owners" connected to the "Artwork" within the "Former Owners" relationship.
We're stuck in querying/displaying this data. What are we missing here?
When displaying related items of a many-to-many relationship, you must use a View.
Given that you want to output the results of this in the loop output of another View, then you will be nesting Views.
It's not clear if you are using the legacy Views editor (where this is straightforward) or the newer Blocks editor (which is less so).
But you need to make a View to query the related posts, where you will add a post relationship Query Filter, and rather than selecting related to the page where the View is shown you will choose related to the current post in the loop.
You then insert that View in the Loop Output section of the first View where you need it displayed. (With the Legacy editor that means inserting the wpv-view shortcode; with the Blocks editor I would also recommend inserting the View that you made elsewhere via shortcode.)
If you need more guidance let me know, I'm not sure how familiar this is to you.
- We're using the legacy views editor currently and are fairly comfortable with toolset generally. Logically I understand what you're explaining on what we need to do, yet something is still a miss in querying the data for the Former Owners Relationship. Making single queries, to a relationship is quite easy, it's when we try to loop relationship data where we hit a brick wall.
- I think it would be easier to focus on querying a single Artwork post to display first before we work on this in other looped views on the site. The aim is to print the Post Titles of each "Former Owner" in the relationship for a single Artwork Post type.
- So for now, how do I display the former owner post titles on a single Artwork Post type?
(attached image of the former owners of an example Artwork Post). The Artwork is a single custom Post type, and the Owner is a single custom Post type in which a "Former Owners" Relationship exists in a "many to many" connection.
(I'll post what I think the settings should be for the view to display the former owners of a single artwork custom post on its specific post page) :
----- Content selection: (Artists Post Type)
----- Query Filter: Select items in the Owners Artworks relationship as related items of... The post where this View is shown
----- Loop Wizard: Unordered list, Post title > A post related to the current post, set by a Types relationship > Former Owners (many-to-many relationship) > "Owners"
- Even in trying to use this shortcode on the single artwork display view - we're still coming up empty-handed.
In this scenario your starting point is a single Artwork post.
You want to display the Owners linked via the "Former Owners" relationship.
So, your View content selection should be Owners.
To this you add a post relationship Query Filter to only return Owners that are connected via the Former Owners relationship to the page (Artwork post) where this View is shown.
Then in the output section just output the post title; the loop output will be iterating over Owner posts, because that's what the View queried, so you just output the post titles for the "current" post.
You'll do the same when it comes to nesting a similar View in the output of a View to display Artworks added to some page, but the difference will be in the settings of the Query Filter. Instead of the current post being the page where the View is shown, the current post will be the current item in the parent loop.
Thank you for clarifying a few things for us.
I think we had a few things wrong in our setup regardless of the issue we found (below).
We've found part of the issue as to why the views were not showing correct output. Part of the reason was that during our testing, we accidentally "deleted" a couple of "Owners" in the relationships on the Artworks Post Edit page. In realizing this deletes the "Owner" altogether, we quickly realized that we need to "restore" the "Owner" on the same page. Even with this restoration, we didn't realize that the restore sets that "Owner" post type to "Draft" and thus would not display in our views output, which set a false expectation that the view was incorrect (it still may have been).
Once we used your suggestion above, and set the "Owner" drafts to "Published" - we then were finally successful.
To Recap:
1) Thank you for your support & suggestion
2) If the feedback doesn't exist for your team:
Please consider displaying "Draft" or some sort of indicator for Relationship connections that are not published and not displayable. I think we could have saved a lot of time understanding that and/or seeing that indication after the "restore" / during the restore.