Skip Navigation

[Resolved] Query related posts by a custom field value

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 3 voices.

Last updated by Luo Yang 1 year, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#2508921

Hi there, I am trying to receive data with using webhook and creating custom posts, depending on the data that I received
I need to create one parent post and multiple child posts. Since these posts will not be created manually. I am not able to create this relationship with Toolset type. both the parent and the children posts are the same unique ID, let's call it as unique-id, so, I think I can use this unique-id to create the relationship and display them but how to?
Parent A, child B, Child C, and Child D. all of them have the same unique-id value. then display all data related to child c, child b, and child D on the Parent A post. Thank you so much!

#2509535

Nigel
Supporter

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

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

Hi there

I'm not sure about the details of what you intend to do. For example, parent and child posts have the same unique ID. I assume you mean that in the data you receive there are posts with the same ID and you want to create parent and child posts within your WordPress site from this data and connect them using Types relationships.

Recall, all posts in WordPress must have a unique post ID, they cannot be the same.

So I expect you receive the data, and you use the WordPress API to publish posts.

You can then use the Relationship API to connect the posts (a parent and some child posts). Specifically, you would use the toolset_connect_posts API function: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

Once you have connected the data, you can display child posts that belong to a parent post by using a View with a relationship filter, see https://toolset.com/course-lesson/displaying-related-posts/

#2509825

Thank you for the response, I guess something that makes confused. Let me take an example with house and rooms. I have received data with webhook for both post types and stored unique fileds like this

House A , house id: 123; room A, house id 123; room b, house id 123.
and
House B, house id 456; room c, house id 456; roomb, house id 456.

My purpose is to diplay each post like. House A includes room A and room B; House C includes room C and room D. Since the data was recived by webhook and were not created manually. I am not able to display posts with Toolset relationship view.
How can I do for this situation?
Have a nice day

#2509827

I don't know coding, this post may tell the method clearly, but I amnot capable of achieving the code 🙂

#2510185

Hello,

It is possible to query posts by custom fields values. see below demo site:
Login URL:
hidden link

1) Create a post view "Related room posts":
hidden link
- Query Room posts
- Filter by:
Select items with field:
house-id is a string equal to VIEW_PARAM(houseid)
- In view's loop, display room information

2) In the single "House" post
hidden link
you can get the custom field "house-id" value with shortcode:
[wpv-post-field name="wpcf-house-id"]

Pass field value to above post view's shortocde, like this:
[wpv-view name='related-room-posts' houseid='[wpv-post-field name="wpcf-house-id"]']

Test it in frontend:
hidden link
It works fine.

More help:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-field
https://toolset.com/course-lesson/filtering-custom-lists-of-posts/#filtering-by-custom-field