Skip Navigation

[Resolved] Sorting posts based on content in a repeatable field group

This support ticket is created 5 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.

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 2 voices.

Last updated by josephC-5 5 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#1345669

Hi there,

I'm building a website for an art gallery. Artwork is a custom post type, and it contains a repeatable field group ("Artwork Detail Group"). The purpose of the repeatable field group is to associate multiple images with a single artwork.

Here's my question. I'd like to list the Artwork posts and sort them based on a field, Artwork Year, in the first instance of the repeatable field group for each Artwork. Is that possible using either Toolset's visual interface or in PHP?

Thank you!

Saul

PS. The website is currently password-protected. I can share the credentials upon request.

#1345751

Dear Saul,

The repeatable field groups are based on one-to-many relationship, so each item of repeatable field group is also a single post.

Views is using WordPress class WP_Query to query posts, since the custom field "Artwork Year" is in repeatable field group, it is not in post type "Artwork", so you can not query "Artwork" posts and sort by custom field "Artwork Year" of other post type.

See WordPress document:
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
Note that a ‘meta_key=keyname‘ must also be present

In your case, I suggest you try these:
1) Create a custom date field "first-artwork-year" in post type "Artwork"
2) Edit each "Artwork" post, setup the value in above date field "first-artwork-year"
3) Then you will be able to setup a post view:
- Query "Artwork" posts
- Sort by custom date field "first-artwork-year"

#1346725

Luo,

Thank you for your response. This is the kind of work-around that I suspected I might need to resort to.

Before giving up, do you know if there is a way to order the results of a post query *after* the query has been run? Basically, I'm seeking the equivalent to the hook posts_orderby (https://developer.wordpress.org/reference/hooks/posts_orderby/) but post-query execution. (And that obviously doesn't change an ORDER BY clause, since the query has finished running.)

Thanks!

Saul

#1346759

There isn't a way to order the results of a post query *after* the query has been run, WordPress is using MySQL query to query the database, the SQL query is running both the post query and order by at the same time. See MySQL document:
hidden link

#1347331

Luo,

Thanks for your reply. I'll create the additional year field as you suggest.

Thanks!

Saul