Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Created November 29, 2017 10:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dingo-d/fb36a80e95dca3b6d02612c1f3d65bbe to your computer and use it in GitHub Desktop.
Save dingo-d/fb36a80e95dca3b6d02612c1f3d65bbe to your computer and use it in GitHub Desktop.
A fix for cutting down the number of queries done by the ACF
<?php
// Assuming we have ACF.
$rows = get_field('images');
// Regular ACF query with 10 images in repeater yeilds 34 queries
foreach ($rows as $row) :
$image_id = $row['image'];
$image = wp_get_attachment_image_src($image_id, 'your-custom-image-size');
echo '<img src="'. $image[0] . '" width="'. $image[1] . '" height="'. $image[2] . '" />';
endforeach;
// Caching the image will give only 16 queries
foreach ( $rows as $row ) {
$ids[] = $row['image'];
}
$cache = get_posts(array('post_type' => 'attachment', 'numberposts' => 5000, ));
foreach ( $cache as $image ):
$image_id = $image->ID;
if ( ! in_array( $image_id, $ids, true ) ) {
continue;
}
$image_src = wp_get_attachment_image_src( $image_id, 'your-custom-image-size' );
echo '<img src="' . $image_src[0] .'" width="' . $image_src[1] .'" height="' . $image_src[2] .'" />';
endforeach;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment