Home › Forums › General Issues › What's faster? › Reply To: What's faster?
Hi again,
Just to make things clearer for my brain ^^
We started the topic talking about two differents datas: the gallery one, and the per attachment ones (when I loop through each image of the gallery)
This is my original code (before speed considerations):
$portfolioItems = get_field('gallery'); // Getting the gallery field
$hiddenMetadata = get_field('metadata_to_hide');
$imagesPerPage = get_field('images_per_page');
$hiddenCategories = get_field('categories_to_hide');
foreach ($portfolioItems as $item) {
// Getting current image informations
$id = $item['ID'];
$filename = $item['filename'];
$width = $item['width'];
$height = $item['height'];
$url = $item['url'];
$alt = htmlentities(urldecode($item['alt']), ENT_QUOTES);
// Getting current image custom attachement fields
$itemCustomFields = get_post_meta($id);
$description = $itemCustomFields['portfolio_description'][0];
$versionName = $itemCustomFields['version_name'][0];
$reference = $itemCustomFields['reference'][0];
$maxWidth = (int)$itemCustomFields['max_available_width'][0];
$maxHeight = (int)$itemCustomFields['max_available_height'][0];
$copyright = $itemCustomFields['copyright'][0];
$rating = $itemCustomFields['rating'][0];
// […]
// Building my output code here
}
Would this one be the optimized? (basically, it’s exactly the same, but with your 4 lines added at the beginning.
$portfolioItemsForCache = get_field('gallery', false, false);
foreach ($portfolioItemsForCache as $itemID) {
get_post_meta($itemID); // 1 query for each image
} // at this point all meta data for all images should be stored in the cache
$portfolioItems = get_field('gallery'); // Getting the gallery field
$hiddenMetadata = get_field('metadata_to_hide');
$imagesPerPage = get_field('images_per_page');
$hiddenCategories = get_field('categories_to_hide');
foreach ($portfolioItems as $item) {
// Getting current image informations
$id = $item['ID'];
$filename = $item['filename'];
$width = $item['width'];
$height = $item['height'];
$url = $item['url'];
$alt = htmlentities(urldecode($item['alt']), ENT_QUOTES);
// Getting current image custom attachement fields
$itemCustomFields = get_post_meta($id);
$description = $itemCustomFields['portfolio_description'][0];
$versionName = $itemCustomFields['version_name'][0];
$reference = $itemCustomFields['reference'][0];
$maxWidth = (int)$itemCustomFields['max_available_width'][0];
$maxHeight = (int)$itemCustomFields['max_available_height'][0];
$copyright = $itemCustomFields['copyright'][0];
$rating = $itemCustomFields['rating'][0];
// […]
// Building my output code here
}
Edit: oh, BTW, the last loads 6x slower than the first, so I’ve obviously messed things up ^^
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.