Skip Navigation

[Resolved] Order posts by date in repeating field group

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

Problem:

The customer has several events, each with multiple dates stored in a repeating field group. They want to display all events in a list ordered by the date of the first event. The inner view correctly retrieves the next upcoming date of each event, but they want the outer view to use the same logic to order the events by the first upcoming date.

Solution:

We explained that due to the way repeatable field groups are implemented (as child posts), the fields for the event dates belong to the child posts, making it impossible to directly order the outer events by these fields. The suggested solution is to add a field directly to the event posts that records the next upcoming event date. This field can then be used to order the event posts. To automate this process, the customer can use the save_post hook to copy the upcoming date to the main post whenever changes are made, ensuring that the data stays up to date.

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

Supporter timezone: America/Sao_Paulo (GMT-03:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by marcialB 3 months ago.

Assisted by: Mateus Getulio.

Author
Posts
#2731917

Hi there

I have several events, each event has several dates (added in a repeating field group). I want to show all my events in a list ordered by the date of the first event date.

It's a nested view: the outer view queries the events, the inner view returns the next upcoming date of that event.

I'd like to use the same logic I use in the inner view to order the outer view.

Thanks for your help!

#2732029

Nigel
Supporter

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

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

Hi there

Because of how the repeatable field groups are implemented, via child posts for each instance of a group of fields, the field values themselves belong to the child posts and not to the original post that the repeatable field group is assigned to.

And you can only order posts by properties of the posts (which is why you can order the date groups by their event date fields). You cannot order the outer events by fields of the inner date groups.

The simplest solution would be to add a field to the event posts that directly records the next upcoming event date, which can then be used to order the event posts.

Although that involves some duplication of data, it is the most effective way to achieve what you need.

You would probably want to automate storing the next date on the event post. If you were submitting content from the front-end only via Toolset Forms, you could likely use the cred_save_data hook to trigger the relevant code, but if the data may be input or edited in the back end (or via import) then you would need to use the more generic WordPress save_post hook.

#2732085

Thanks for your suggestions. I'll try to use the save_post hook to copy the upcoming date to the main post.