I am working on a website that needs to have gallery with lots of images (200-300 in each gallery). As part of optimizing the load time of a post containing such a gallery in the front-end, I would like to just get the IDs of the images that I have selected in the gallery and not the formatted array that ACF returns.
The problem is that this formatted array returned by ACF requires a lot db queries, which makes the response for a post with a lot of images in its gallery quite slow.
If I can manage to get the IDs, my idea is to load only some images in the beginning and the dynamically get more images via AJAX, as the user scrolls down the page.
I had a look in the source and particularly the format_value
function of class-acf-field-gallery
but there are no filters there that I can hook into. At the same time I cannot really understand whether the acf/format_value
runs before or after the value has already been formatted. If it runs after then it means that all the db queries I want to avoid have already been run.
I hope the above makes sense! Any help pointing me towards the right direction will be appreciated! Thanks.
The gallery field is stored in the database as a serialized array of attachment IDs.
You can use native WordPress’ function to get those IDs in a form of simple one-dimensional array:
get_post_meta( $post_id, 'gallery_field_key', true )
Thanks so much for the reply and the elegant solution Ihor! That’s exactly what I was looking for.
I really don’t know why I was thinking of a complex solution, while it was something so obvious!
@psymeons you’re welcome 🙂