Skip Navigation

[Resolved] Creating a view to embed with shortcode in Elementor using relationships

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

Problem: I have a post relationship set up between two custom post types. On the single post page for one of the custom post types, I would like to display a list of all the related posts.

Solution: Use Query Filters in this View to display only those posts that are related to the current post being displayed.

Relevant Documentation:
https://toolset.com/course-lesson/what-are-post-relationships-and-how-they-work/
https://toolset.com/course-lesson/how-to-set-up-post-relationships-in-wordpress/
https://toolset.com/course-lesson/many-to-many-post-relationships/
https://toolset.com/course-lesson/displaying-related-posts/

This support ticket is created 3 years, 11 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 CaseyK4294 3 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1867305

Tell us what you are trying to do?
I have two custom post types, Towns and Real Estate Agents. I set up a many to many relationship between these two custom post types. Up to five towns can be related to one real estate agent, but an unlimited number of real estate agents can be assigned to any one town. The Town template page is created using an Elementor template. I want to create a view in Toolset Content Templates that will display featured real estate agents in the town templates. It should only display the real estate agents that have a relationship to that town. Then I want to show the view using the shortcode widget in Elementor. I was able to create the view and embed it, but not to limit it to the real estate agents that have a relationship with that particular town.

Example: I'm working with 3 published towns (Bridgewater, Raritan and Somerset) and 2 published Real Estate Agents (Casey Kittel and Nicky Vinos). Casey has a relationship with all three towns, Nicky only has a relationship with one town, Raritan. In the link I provide below for the town Bridgewater, you can see she is still listed.

Is there a way within the content template editor to filter the results of the view to only show the related Real Estate Agents for the current Town page?

What is the link to your site?
Bridgewater: hidden link

#1868203
Screen Shot 2020-12-07 at 2.14.18 PM.png

Hello, in this case you need to add a Post Relationship Query Filter to your View. You can configure the filter so that only those posts related to the current Town being displayed. If you cannot see the Query Filter panel in your View editor screen (legacy Views editor), you can turn it on by scrolling up to the top and clicking "Screen Options". You can enable the Query Filter panel there. Once the panel is visible, add a new Post Relationship filter and select your Town and Agents post relationship. Choose the option "as related items of"..."The post where this view is shown" - see the attachment here. This configuration will filter out all the Agents that are unrelated to the current Town post in the M2M relationship you select in the filter configurations.

If you're using the Block Editor to create this View, you can find the Query Filter section inside the Content Selection configuration panel when you select the top-level View block.

Let me know if you have questions about this.

#1868353

Thank you! I am using the blocks editor and thanks to your help, I was able to figure out how to set it to function the way I wanted it to. I do have a follow up question about this particular view and additional settings. If I limited it to show only 1 real estate agent in the view, but there are multiple real estate agents with a relationship to the town, is there a way to set it to randomly feature a different agent each time the page is loaded? As an alternative option, is there a way to make it show all related agents, but have them appear in a different order in the view each time?

#1869045
Screen Shot 2020-12-08 at 9.47.46 AM.png

Thank you! I am using the blocks editor and thanks to your help, I was able to figure out how to set it to function the way I wanted it to.
Great!

If I limited it to show only 1 real estate agent in the view, but there are multiple real estate agents with a relationship to the town, is there a way to set it to randomly feature a different agent each time the page is loaded? As an alternative option, is there a way to make it show all related agents, but have them appear in a different order in the view each time?
Yes, either of these random ordering setups is achievable in the Block Editor. If you only want to show one random related agent, you can leave the 1 result limit applied, then adjust the Ordering Settings for the top-level View block to be "Random order". See the attachment here. You must have the top-level View block selected to modify this Order Setting. Use the Block Navigation menu at the top of the screen (the icon looks like 3 stair-steps) to select the top-level View block, or hover over block toolbar above each block. The parent block icon will appear above the toolbar, and you can click it to navigate up the block hierarchy one level at a time.

If you want to show all the related Agents, but order them randomly each time the page is loaded, you would follow the same process to change the Ordering Setting to "random order", but then you would also remove the 1 result limit you applied earlier. This would display all related Agents in random order each time the page loads.

#1869253

My issue is resolved now. Thank you! Amazing support team!