Skip Navigation

[Resolved] Group by repeating custom (date) field and sort by date

This support ticket is created 7 years, 1 month 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
- 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 6 replies, has 2 voices.

Last updated by ivoR 7 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#503876
screenshot-localhost 2017-03-21 19-23-43.png

I am trying to:
I have a Field Group to create Events. Events can have multiple dates, so I've made a repeatable custom field 'datum' (date) .
What I would like to achieve on the frontend: show events, ordered by date.

My Loop

		<wpv-loop>
          <div>
			[wpv-for-each field="wpcf-datum"]
				[types field='datum' style='text' format='F j, Y g:i a' separator=', '][/types]
				<strong>[wpv-post-title]</strong>  <hr />         
            [/wpv-for-each]
          </div>
		</wpv-loop>

It gives me a list of Events, split by date. Great. But I want to order them by date! How? (yes, I've set 'order by' to ' wpcf-datum'

#504067

Hi,

There isn't such a built-in feature within Views shortcode [wpv-for-each], see document:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-for-each
There isn't any option can sort the post fields.

I suggest you create a custom shortcode for it:
https://codex.wordpress.org/Function_Reference/add_shortcode
You can retrieve the custom field value with wordpress function get_post_meta()
https://codex.wordpress.org/Function_Reference/get_post_meta

#504085

Could you make that for me? I can pay for it and you can share it here for other users.

#504167

There is a misunderstand, I assume you are going to create a view to query the "Event" posts, and order the view by custom date field "datum", which is a multiple instance field.

The Views plugin is using wordpress class WP_Query to query the posts, if you setup the view order by the multiple instance field "datum", WP_Query will take the first item of field "datum" from each post as the order by value, so it will conduct some unexpected result.

In your case, I suggest you create a single instance field + a child post type, for example:
1) Parent post type "Event", with a single instance date field "main-datum"
2) Child post type "Event-date", with a single instance date field "datum",
Then you can create a nested view:
1) Parent view list posts of post type "Event", order by the custom field "main-datum"
2) Child view list "Event-date" posts, filter by:
Select posts that are a children of the current post in the loop.
see our document:
https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/

#504173

I think I'll end up with the same problem.
Let me explain it better.

I have Events:
Event A. Will take place on march 01 2017 and juli 16 2017
Event B. Will take place on may 05

I want to show upcoming events, ordered by date. Like:
- march 01: Event A
- may 05: Event B
- juli 16: Event A

If I order by 'main-datum' as you suggest I guess I'll end up with
- march 01: Event A
- juli 16: Event A
- may 05: Event B

#504182

You can try as I mentioned above:
create a single instance field + a child post type, for example:
1) Parent post type "Event", without the custom date field.
2) Child post type "Event-date", with a single instance date field "datum",
Here you just need one view, to query the child "Event-date" posts, order by the date field "datum", and display the parent "Event" post link + value of date field "datum".

To display parent post information, please check our document:
https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/

#504301

I've tried what you suggested and I'm happy with the output. 🙂 Thanks!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.