Home Forums Add-ons Gallery Field Convert Attachments to Gallery


Convert Attachments to Gallery

  • Does anyone have a script i can run to convert/create a gallery from existing post attachments? I have 160 posts from about 5 years ago with existing galleries generated from attachments before wordpress started using gallery item IDs.

    Seems like a function MANY people would need to migrate old sites? Surprised there isn’t a plugin – maybe there is?

    So… I guess for each post I’ll need to create an array of attachment ids, a _gallery field_0000000000 and format the serialized data using update_field() just not sure how to create the field programatically.

    So if anyone has this code handy or can write it quickly that’d be great!

    Otherwise I’ll create a support ticket on Monday.

  • This is on the right track
    – What I’ve done is just write some PHP code and hooked it to admin_init
    – I’m unfamiliar with ACF

  • Moving from one way of doing things to another is never an easy process.

    WP holds gallery shortcodes in the content area where ACF stores galleries in a separate field. There really isn’t an automated way that you can convert from one of these to the other. Not that it isn’t possible, but because it would be time consuming and will probably time out the site if you try to do it on a page load. You need to look in the content of every post, see if there is a gallery, see what images are in it (are they all the attached images or selected ones), update the ACF field that should now hold the gallery and then delete the gallery currently in the content editor.

    The best way to do this would be to leave those galleries alone and build the new one using ACF and then in the template file that show them to build conditional logic that can use either one of them depending on what’s found in each post. Still complicated, but doable.

    My choice would probably be to not do anything as long as the old galleries are still working or there is some really good reason that it needs to be converted. Any way you look at it this will be a fair amount of work because, like I said, it’s not something that can really be automated, or at least not easily, which is likely why you can’t find anything that does it.

  • Hi John,

    Thanks for replying.

    I’ll just repeat the critical part:
    Convert Attachments to Gallery:
    “I have 160 posts from about 5 years ago with existing galleries generated from attachments before wordpress started using gallery item IDs.”

    When I say “galleries” I don’t mean WP_galleries because they didn’t exist (using IDs) pre 3.5 They are purely the attachments with common post_parent. They were either uploaded directly to the post using “add media” or “attached” using a utility called file gallery which is not user friendly to say the least – hence the need for changing to a better system. I am currently showing either (attachments) or (acf gallery) depending on the post using an if statement.

    I can see a few people have created galleries programatically from an array of image IDs (example above). I think all i need help with is generating the _gallery meta_value.
    “using update_field() just not sure how to create the field programatically.”

    Can you help with that? 


  • You could create an acf/load_field filter or an acf/load_value filter You could then use the WP get_attached_media() function to get all of the media for a post.

    add_filter('acf/load_value/name=your-field-name', 'build_gallery_from_attachments', 10, 3);
    function build_gallery_from_attachments($value, $post_id, $field) {
      if (!empty($value)) {
        // already has a value
      // does not have a value, build value from attachments
      $media = get_attached_media('image', $post_id);
      $value = array();
      if ($media) {
        foreach ($media as $image) {
          $value[] = $image->ID;
      return $value;
  • Hi John,
    Thanks, but I’m unsure how to apply this to my 160 posts automatically.

    Ideally I’d run a function once – that checks each post and adds the gallery.

    I’ve contacted support as the only part i really need help with the the creation of a new ACF gallery meta_key and meta_value.


  • This will do the following.

    1) If a post has not been edited yet it will cause the front end output of the gallery field to automatically hold all of the image attachments for the post.

    2) When a post is updated in the admin it will automatically load all of the image attachments for the post and put them into the gallery field where they can be edited and saved.

    Like I said in my first reply, there is not automatic way to have this happen on all existing posts. This is the same as any ACF field. If you have posts that already exist, creating a new field does not automatically set values in those already existing posts. There is nothing in ACF or WP that will do that.

  • Thanks,

    I did try your code, but it didn’t seem to do anything to the existing post attachments on update.

    I’m surprised you think this can’t be batch processed. I guess I’ll die trying 😉

  • What I posted was not designed to retroactively update all of the posts. It is designed up return the correct values when using get_field() or to load the values as a default when editing a post. This won’t do anything to the existing posts

    Most people are looking for something they can just run by loading a web page. It could be batch processed with a cron or if you created way to run it that will not time out where you can set the max execution time. Other than telling you that you could do this and you would use update_field to update the values of every post, I don’t have code that will do this. Since the update_field() function will require the field key of the field you want to update it’s not something that is generic.

    1) Run a query to get every post
    2) Loop through the posts
    3) Get all attachments for each post
    4) Call update field using the field key, the value will be an array of post IDs.

    With 160 posts it is very likely that this will time out if you try to run in through a browser.

    You might be able to speed up the process using $wpdb and do the db queries yourself, that would be beyond what I can help you with.

  • Fortunately I’ve got a workaround.

    If the client wants to edit a particular gallery…
    they just need to:
    add to gallery > select all attached (“uploaded to this post”).

    done √

    Thanks for your suggestions tho

  • I used Johns code and it worked perfectly. Except I think you missed a ! on the if(empty($value)){ line. Should be

    if (!empty($value)) {

  • @lukeseall you are right, I’ve corrected my previous comment.

Viewing 12 posts - 1 through 12 (of 12 total)

The topic ‘Convert Attachments to Gallery’ is closed to new replies.