corcel / acf

Advanced Custom Fields (ACF) plugin for Corcel
127 stars 100 forks source link

Gallery field order #50

Closed omzy closed 6 years ago

omzy commented 7 years ago

I have added some images with the gallery field. I then re-ordered the images in the admin, however on the front end it is still showing the images in the previous order. In the admin I can see the images are in the new order after refreshing the page.

Is this an issue with corcel or with acf?

I output the images as follows:

@foreach ($post->acf->gallery('article_images') as $image)
     <img tag here>
@endforeach
omzy commented 7 years ago

Just been looking in to this and I think the issue is on line 39 in Gallery.php:

$attachments = Post::on($connection)->whereIn('ID', $ids)->get();

When you do a SQL IN query, it does not return the results in the order that you specify. This is default behaviour of SQL:

https://stackoverflow.com/questions/1631723/maintaining-order-in-mysql-in-query

I think we need to find a way to fix this because the JSON API returns the results in the correct order.

omzy commented 7 years ago

This works:

$ids_ordered = implode(',', $ids);

$attachments = Post::on($connection)->whereIn('ID', $ids)
     ->orderByRaw("FIELD(ID, $ids_ordered)")->get();

Reference: https://stackoverflow.com/questions/26704575/laravel-order-by-where-in

omzy commented 6 years ago

What do you think @jgrossi?

jgrossi commented 6 years ago

@omzy83 go ahead and send a PR ;-) thanks

omzy commented 6 years ago

@jgrossi PR is here: https://github.com/corcel/acf/pull/56