Home › Forums › Front-end Issues › Optimize code to query Custom Fields as PHP memory limit error?
Hi there,
My client is experiencing a PHP memory limit error on one of the pages and I was wondering whether it is related to the way I am calling the Advanced Custom Fields. Sorry, I am not an expert in PHP, so I was wondering whether this code is repetitive and could be optimized:
<div class="container post">
<?php
while (have_posts()) :
the_post();
?><?php endwhile; ?>
<div class="row-fluid post-title">
<header>
<p class="entry-title"><?php the_title(); ?></p>
<p class="entry-sub-title"><?php the_subtitle(); ?></p>
<!--<?php get_template_part('templates/entry-meta'); ?>-->
</header>
</div>
<div class="row-fluid info-table">
<div class="info-details">
<?php
$key = 'xxx';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != '') {
echo 'xxxx';
} ?>
<?php if( get_post_meta($post->ID, 'xxx', true) ) { ?>
<span class="info-detail"><?php the_field('xxx'); ?></span>
<?php } ?>
<?php
$key = 'yyy';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != '') {
echo 'yyy';
}?>
<?php if( get_post_meta($post->ID, 'yyy', true) ) { ?>
<span class="info-detail"><?php the_field('yyyy'); ?></span>
<?php } ?>
<?php
$key = 'zzz';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != '') {
echo 'zzz';
}?>
<?php if( get_post_meta($post->ID, 'zzz', true) ) { ?>
<span class="info-detail"><?php the_field('zzz'); ?></span>
<?php } ?>
etc.
Am I making too many PHP queries and how could it be optimized?
Thanks so much for any help and best wishes.
Hi @caram
You could better optimize the code by making use of the get_field
function instead of the get_post_meta function. This will allow ACF to cache data and only load it once (instead of twice in your case):
<?php if( get_field('yyy') ) { ?>
<span class="info-detail"><?php the_field('yyyy'); ?></span>
<?php } ?>
This said, your code should not be maxing out memory limits, so it’s more likely to be a ‘too many plugins / functionality’ issue. Perhaps a server upgrade is needed or you can remove some plugins / functionality.
Thanks
E
The topic ‘Optimize code to query Custom Fields as PHP memory limit error?’ is closed to new replies.
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.