Support

Account

Home Forums Add-ons Gallery Field Gallery field to save image order in database?

Solved

Gallery field to save image order in database?

  • Hello,
    We’ve been using the addon for adding galleries successfuly for a while but now we need to run multiple XML exports to other sites. The problem is all images are randomly placed in the XML we generate and they rarely appear in the way we have arranged them. I was wondering is there a way to save the image order into the database so all images will always appear in the way they are initially set up?

    Thanks a lot!

  • If you’re exporting to XML, it depends on what you’re using, but it’s probably outputing them in the order that they were uploaded to WP. Gallery images are WP Media files and these are posts that are exported. The only way to get them in the same order is if the tool you’re using to export to XML reads the value of the ACF field to reorder post IDs before output.

  • Thanks for your response!
    We are using our own php code in order to generate the XML. Do you have any idea how can we set it up to read the value of ACF field?

  • 
    // post ID in second arg
    // false in 3rd arg gets the unformatted value
    // this will return an array in the order that the images
    // are in the gallery
    $gallery = get_field('your-gallery-field', $post_id, false);
    // use this array to query the attachments
    $query = new WP_Query(array(
      // other query arguments
      'post__in' => $gallery,
      'orderby' => 'post__in'
    ));
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Gallery field to save image order in database?’ is closed to new replies.