Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Order to ACF multiple value in loop query #5

Open
ghen-arvesu opened this issue Oct 30, 2024 · 1 comment
Open

Add Order to ACF multiple value in loop query #5

ghen-arvesu opened this issue Oct 30, 2024 · 1 comment
Assignees

Comments

@ghen-arvesu
Copy link

ghen-arvesu commented Oct 30, 2024

Hi @eliot-akira can we have a custom order for ACF with multiple values such as acf_post, acf_relationship, acf_select, and other fields that often store values, and having the ability to order queries based on these arrays?

An example of maybe something like below:

<Loop acf_post=my-post-name orderby="title" order="desc">
  <Field title />
</Loop>
<Loop acf_select=my-select-name field=labels>
  Label: <Field />
</Loop>
<Loop acf_repeater=my-repeater-name>
  <Field title />
</Loop>

Which could be based on the values below
image

this is related to a post on https://tangibletalk.com/t/acf-repeater-loop-filtering/1275

@eliot-akira
Copy link
Contributor

eliot-akira commented Nov 11, 2024

Hi @ghen-arvesu - Thank you for making a note of this. The feature description and example usage is a great reference for developing the feature, I appreciate it.

ACF field loops with multiple values are usually based on the List loop type, which supports sorting by field with:

  • sort_field, sort_order, and sort_type

They're briefly documented here. https://docs.loopsandlogic.com/dynamic-tags/loop/#attributes-1


I think the difference needs a better explanation in the docs, maybe the topic deserves its own page.

  • The query parameter orderby is supported by the post loop type, which passes it to WP_Query to make a database query. Similarly, the taxonomy loop type with WP_Term_Query and user loop with WP_User_Query. Each of these is a separate implemenation in WP core with some differences in accepted values.
  • The generic loop parameter sort_field is supported by the base loop, which all loop types are built on. Any loop type with multiple values, including the result of post queries, can be sorted as long as it's a list of objects with sortable field values (like number, string, date).

For example:

<Loop acf_post=my-post-name sort_field="title" sort_order="desc">
  <Field title />
</Loop>

ACF Repeater probably can't be sorted this way, since it's a list of various field types.


..Looking at the code where the ACF Post loop is created (here), it gets the list of post IDs from ACF and creates a post loop, not a generic list. So the orderby query parameter is supposed to be working. I'll look into what's happening there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants