Home Forums Feature Requests Reduce Queries – Save Image URL not ID


Reduce Queries – Save Image URL not ID

  • The problem: All of the wp_get_attachment functions require 2 database calls per image (from what I can tell by testing it out), so that means any image referenced by an ACF field += 2 queries. This can be a lot if you have a fairly large repeater (or a gallery).

    Since we know what image we could save the URL (and any other associated data) in a post_meta field instead of only saving the attachment ID.

    I know this is possible by using a text field instead of an image field, but then the image selector is lost, and it’s harder for the WP admins/editors to input images.

    Solution: What I think would be great is a new field that lets you use the image selector to saves the URLs (full and all pre-sized ) and meta in the post_meta table, perhaps serialized into a single field.

    Has anyone attempted to build a field that does something like this? Is it a foolish idea?



    • 2 less queries per image


    • Less adaptable to any changes to the attachment
    • Not backwards compatible – requires a new field
  • Hi @wilirius

    I think you can create a text field that will hold the serialized image data. After that, you can use the acf/save_post hook to update the serialized image text field when you updating the post. You can update the field using the update_field() function.

    I hope this helps.

  • Ahh yes that’s a great idea! I think I saw something like that before for saving the GPS lat/lng from the map field.

    Just to verify:

    • Hook in to the acf/save_post
    • Check for an update to the image selector field
    • Update a separate text field with the URL for the image from the image field
  • Hi @wilirius

    I think that’s perfect. Don’t forget that you can use serialize() and unserialize() functions.

    I hope this helps 🙂

  • Definitely helps, good solutions & info, thanks a bunch.

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

The topic ‘Reduce Queries – Save Image URL not ID’ is closed to new replies.