Skip Navigation

[Closed] Query syntax for showing related content using one to many relationship

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

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 3 years, 3 months ago.

Author
Posts
#2184219

Tell us what you are trying to do?
I have two custom post types:
1. Watershed
2. Place
Watershed is the parent. On a watershed single post I am looking to show the related Places.

In other words:
Watershed X post shows Place A, B and C.

I know I can use Views to do this but I am trying to do this in Elementor using a grid builder widget (Crocoblock Grid Tiles) which will allow me to use a custom query. However I do not know the correct code to insert into the custom query. I thought perhaps I could build in Views and then grab the code but I haven't been successful.

I have read at least 10 similar posts but they are different enough to be too confusing.

Here is an example watershed post, the 3 images in the grid at the bottom ARE indeed places but they are ALL places not ones from this watershed:
hidden link

Thank you,
William

#2184469

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

If you are not using Views and instead using a custom query, you need to run the query in two parts.

First use toolset_get_related_posts to retrieve the IDs of the child Place posts.

Then use those IDs to populate the post__in parameter of your get_posts (or WP_Query) call.

See

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

Alternatively you can provide parameters directly to a single WP_Query, as described here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts

I'm not familiar with how you set up the custom queries in Elementor, but the above form the basis of how to proceed from our end.

The topic ‘[Closed] Query syntax for showing related content using one to many relationship’ is closed to new replies.