Support

Account

Home Forums General Issues Single Call to Database

Solved

Single Call to Database

  • Hi Elliot,

    I was wondering…is every instance of get_field(), the_field(), the_sub_field() etc. a separate call to the database?

    I’m assuming it is and was wondering if there is a way to optimize this. Something like:

    global $post;
    $post_meta = get_post_meta($post->ID);
    
    <?php if($post_meta['news_update']): ?>
        <ul id="ticker">
            <?php foreach ($post_meta['news_update'] as $news_update): ?>
                <li>
                    <?php echo $news_update['news_update_text']; ?>
                </li>

    Versus this:

    <?php if(get_field('news_update')): ?>
        <ul id="ticker">
            <?php while(has_sub_field('news_update')): ?>
                <li>
                    <?php the_sub_field('news_update_text'); ?>
                </li>

    Would that work? And how would you handle sub fields?

    Thanks for your time!

  • Hi @revconcept

    Yes, you are correct in pointing out that the above code would be more efficient. However, with that, there are many drawbacks, such as values will not be formatted by the ACF fields, etc.

    Thanks
    E

  • Hi @revconcept

    Sub fields would simply not work, as they would not be formatted correctly.

    Thanks
    E

  • If performance is an issue, I would look into page caching before changing all of your PHP ACF code.

    Thanks
    E

  • That’s what I figured. Thanks for the response…you’re always so prompt!

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

The topic ‘Single Call to Database’ is closed to new replies.