Skip Navigation

[Resolved] Get Parent Post

This support ticket is created 6 years, 7 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 6 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#909100

I am trying to: I have a Course and each course has different dates and locations. What would it be the best approach? Create 2 types, one for the course and the other one for the course locations and make the relationship between them (types 3.0)? Or create a field repeating group locations for a post type Course?

I'm using PHP template to display the results, and for the first situation when I tried to get the parent post id to be able to filter the results its return 0.

$event_id = toolset_get_parent_post_by_type( get_the_ID(), 'event' );

The second situation, the only help I could find to display the group is thru views and I want to handle it in my PHP file. How can I display the repeat group information?

Please let me know if you can help me.

#909235

Nigel
Supporter

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

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

It seems like you should be using a many-to-many relationship here, so that courses can be offered in more than one location, and each location can provide more than one course.

If you used a repeating field group for locations then each location would belong to a single course post, and if you wanted to add the same location to another course you would end up duplicating the location. It would be more like a one-to-many relationship in that case.

To retrieve related posts from a many-to-many relationship you need to use the API function toolset_get_related_posts (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts).

Pay careful attention to the arguments. The 8th is required, meaning you have to supply at least the first eight.

#909399

Hi Nigel,

I still not sure if many to many will be the best way to go since I have in the location type other information like date, time, etc. I understand your point. Now, for me to test it out the group fields, how can I handle to display it thru PHP instead of using views?

Thanks,
Adriana

#909810

Nigel
Supporter

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

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

When you add a repeating field group you are actually creating a new post in wp_posts for each repetition, and the field values themselves are stored as custom fields of that post in wp_postmeta.

The post_type of these container posts is the slug of the repeating field group.

In the screenshot I have a repeating field group with a slug of project-tasks.

You can't simply use get_posts (or WP_Query) to retrieve such posts because you would return *all* such posts and not only those to which the repeating field group belongs.

That "belongs" is stored in the custom relationship tables, accessible with the same relationships API.

So you will still need to use toolset_get_related_posts to retrieve the IDs of the container posts, and then for each of those you can use the standard get_post_meta to retrieve the raw field values (or use types_render_field for the formatted values).

With my project-tasks RFG I could retrieve the container posts using just the following (I only needed to specify the first 3 arguments as the defaults work for the remainder):

$ids = toolset_get_related_posts( get_the_ID(), 'project-tasks', 'parent' );