Support

Account

Home Forums Front-end Issues Optimize code to query Custom Fields as PHP memory limit error?

Solved

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

  • Thanks so much for your advice, Elliot!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Optimize code to query Custom Fields as PHP memory limit error?’ is closed to new replies.