Skip Navigation

[Resolved] Display Relationship Fields

This thread is resolved. Here is a description of the problem and solution.

Problem: I have two custom post types - Articles and Authors - connected in a many-to-many (m2m) post relationship. I have created a View of Articles posts, and in the list of Articles I would like to include information about the related Author.

Solution: Any time you want to display information from the "many" side of a post relationship, you'll need a View. Since the relationship between Articles and Authors is a many-to-many relationship, you'll need a nested View structure to display a list of Articles and the related Authors for each Article.

Nested View creation is not directly supported in the Block Editor, so you can use the legacy View editor if necessary. For the inner View, use a post relationship Query Filter configured with the Articles - Authors M2M relationship selected, showing items as related to the current post. You can place the inner View in the loop of the outer View using a wpv-view shortcode.

This support ticket is created 2 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by stevenM-7 2 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#2119979

Tell us what you are trying to do?
I have 2 custom post types - Articles and Authors. There is a many to many relationship between these post types. I am trying to display a listing of Articles (which I did) but also show the related Author for each article on this listing.
Is there any documentation that you are following?
I have been trying to google and follow other help/support articles but I havent had much luck. This data and custom post types was imported from the current site so I'm wondering if something is missing or didn't import correctly which is why this isn't working properly for me.
Is there a similar example that we can see?
This is what I want it to look like: hidden link I have the listing without the author here: hidden link
What is the link to your site?
hidden link I created a stage site because All of the data was imported from their current site and I was worried that I might lose data by changing the relationships.

#2120479

Hello, any time you want to display information from posts in the "many" side of a relationship, you'll need to use a View, since more than one post could be displayed. You have a many-to-many relationship here, so you'll need a View of the Author post type to display information about the Authors related to each Article.

The problem here is you want to display that information in a View of Authors, and it is not possible to create nested Views using the Block Editor. Are you familiar with the legacy View editor? You'll need to create this nested View using the legacy editor, then insert it in the View of Articles using a shortcode. If you're not familiar with the legacy editor, I can walk you through the process.

#2121253

I think I have gotten close using what you told me, however I'm still not quite there. I was able to create listing views for Authors and articles using the classic editor. My Author view is successfully working on article detail pages: hidden link (I only have the title and author displaying so far). However I still dont have the author showing on the listing: hidden link. It just says "No Items Found". These are screenshots of the 2 views - Authors: hidden link & hidden link And then my Article View: hidden link Are you able to see what I am missing?

#2121419

Great, it looks good to me, so far. I think the issue in the Law Page is the post relationship filter settings of the Author View, and the fact that the Author View is displayed in two different contexts: one context is in a page "Law," and the other is in a View of Articles posts. The post relationship filter is currently set to find posts "related to the post where the View is shown". While that filter setting works well when the Author View is displayed in the context of an Article post or nested in another View which is displayed on the Article post, it will not work if the Author View is displayed in the context of a different page (like "Law", for example). In that case, "the post where the View is shown" refers to the Law Page. Apparently no Authors are related to the Law page in any post relationship - so you see "No items found". My first suggestion is to try the option "the current post in the loop" to see if this produces the correct results in both contexts. If not, you need to create another Author View with different Query Filter configurations.

To display a View of related Authors properly inside an Article View, the post relationship Query Filter in the nested Author View should be configured based on"the current post in the loop" instead of "the post where the View is shown". You can duplicate the current Author View in Toolset > Views and make minor adjustments instead of creating a new View from scratch. To duplicate the View, go to Toolset > Views, hover your mouse over the current Author View title, and click the "Duplicate" link that appears below the title. Give it a similar name, but include something like "in Articles Loop" in the title/slug to indicate the difference between the two similar Author Views. Edit the new View, and in the Query Filter settings for the post relationship filter you should set it to be based on "the current post in the loop" instead of the "post where the View is shown". Then update the existing Article View to display the new Author View instead of the old one. Find the wpv-view shortcode used to insert the old Author View, and replace the View slug "authors" with the slug of the new duplicate View. That should fix the display on the Law page, and anywhere else the Articles View is displayed.

Also, it's usually a good idea to choose a specific post relationship instead of "any relationship" in post relationship Query Filters unless you really do want to include posts from other post relationships here.. In your current site it might not cause any problems, but in complex sites with multiple post relationships involving the same two post types, you could see unexpected results.

If these changes do not solve the problem, I'll need to take a closer look.

#2121489

When I changed the Author View to "the current post in the loop" all of my listing started working correctly. I have a lot more work to do to build out the rest of the site but I think (hopefully) I should be able to figure everything else out with what you have helped me with. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.