-1

I have created a simple headless WP REST API to manage some data to an app I am creating. Per this post I have implemented that filter plugin to re-allow filtering with ACF and the REST API. Therefore, I am able to create a query that sorts the data (note that I am using custom post types as well as custom fields) via title like so: ?filter[meta_key]=title&filter[order]=DESC

But if I try literally any other field, it gives very inconsistent sorting. The biggest issue is trying to sort via dates. ?filter[meta_key]=release_date&filter[order]=DESC

The goal is to do this by only using plugins so that I can focus on any code I write being in the app itself and manage the plugins and leave the rest of the WP instance alone. Therefore I'd prefer a solution that is plugin based or requires forking/editing a plugin. Perhaps I'm just trying to filter things improperly.

I use the following plugins (amongst a few unrelated plugins):

Advanced Custom Fields PRO
ACF to REST API
Custom Post Types UI
WP REST API Controller
WP REST API filter parameter

EDIT: After reviewing this some more, I realize now that the FILTER plugin from the post mentioned above doesn't seem to be working and therefore ACF orderby doesn't work. Is there any way to re-enable orderby with ACF via plugin?

closed as off-topic by Michael, Mark Kaplun, Nathan Johnson, Christine Cooper, Max Yudin Feb 25 '18 at 14:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • if you need data to be formatted in some specific way you should simply write your own end point – Mark Kaplun Feb 22 '18 at 6:32
  • @MarkKaplun Sorry, I'm not sure what you're referencing. The issue is improper sorting, not data formatting. The dates are returned in YYYYMMDD format which should be easily sortable, but the filter/orderby params don't seem to be working properly with ACF fields. Figured this was surely an issue that someone else had encountered but it doesn't seem to be the case :( – Russell Ingram Feb 22 '18 at 15:43
  • let me try again, if you need anything that is not supported by the official API "out of the box", you should write your own end point. – Mark Kaplun Feb 22 '18 at 16:16
  • Ok, thanks, I guess. Just figured this would be a more common issue and that someone would have encountered it before and tried a common solution instead of thousands of people adding their own snippets to WP. WP dev at its best, I suppose. – Russell Ingram Feb 22 '18 at 16:57
  • you are using a plugin (actually two) and the quirks of it are outside of the scope of this site. – Mark Kaplun Feb 22 '18 at 16:59
1

What "return format" do you have set in ACF for the date field? Sorting by numbers produces unexpected results when you use "d/m/Y" or "m/d/Y". If you instead choose "Ymd" as in "20180221" for the return format, you'll get an integer that's sortable in the expected way.

  • Sorry, I meant to add this to the OP and forgot. I do have it in Ymd which is why I expected it to work. I realize now after trying a few more things that for some reason none of the ACF fields are working, note just the date, and that this isn't a date specific issue. – Russell Ingram Feb 21 '18 at 23:17
  • ACF doesn't expose itself in the REST API by default - you'll need another plugin for that: wordpress.org/plugins/acf-to-rest-api – WebElaine Feb 22 '18 at 14:24
  • Thanks Elaine, I do have the ACF to REST API plugin included. The ACF fields appear in the REST API but they do not get sorted properly via the orderby filters that I use. I also know that filter doesn't work by default anymore, so I used another plugin (WP REST API filter parameter) to re-enable this. This allowed the title to be sorted via orderby but it doesn't seem to sort properly for any of the ACF fields. – Russell Ingram Feb 22 '18 at 15:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.