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

Orderby ACF not working #382

Open
Elwazeery opened this issue Jul 9, 2021 · 1 comment
Open

Orderby ACF not working #382

Elwazeery opened this issue Jul 9, 2021 · 1 comment

Comments

@Elwazeery
Copy link

Hello
I tried to order the posts by acf but I see it's not working
https://mysite.win/wp-json/wp/v2/cars?filter[orderby]=star&filter[order]=desc
I don't know what is wrong here

@leok85
Copy link

leok85 commented Dec 1, 2021

I'll answer here that maybe I can help other people.

For example I have a post type called events and I want to sort by date field.

add_filter( 'rest_events_query', function( $args ) {
  if ($args['orderby'] === 'date') {
      $args['meta_key'] = 'date';
      $args['meta_type'] = 'DATETIME';
  }

  return $args;
});

API URL: .../acf/v3/events?orderby=date&order=asc

I also have this other function that I don't know if it's really necessary. In some places in my application I need to order by "menu_order" (Post Types Order plugin) and in other places by date

add_filter( 'rest_events_collection_params', 'filter_add_rest_orderby_params', 10, 1 );

function filter_add_rest_orderby_params( $params ) {
  $params['orderby']['enum'][] = 'menu_order';
  $params['orderby']['enum'][] = 'meta_value';
  return $params;
}

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