Created
November 29, 2017 10:21
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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