Support

Account

Home Forums Add-ons Flexible Content Field Best Way to set ACF variables for Flexible content

Helping

Best Way to set ACF variables for Flexible content

  • Hello,

    I working on building a make shift page builder for a theme. I am doing this by creating multiple layouts within a Flexible content field and then allowing the user to add as many of each layout as they want to build out a pages content.

    While I have it working right now I am noticing that once a full page is built out with multiple layouts the query count starts to get pretty high. I have been looking around for ways I can bring this down but right now I feel as if I am going in circles.

    Before I ask my question I wanted to post my main php file that is building out the page. This will give you insight into how I am handling each layout.

    <?php
    /*
    Template Name: Main Builder
    */
    
    locate_template( 'templates/header.php', true, true );
    	// start loop
    	while ( have_posts() ) { the_post();
    		if ( have_rows('main_builder') ) {
    			echo '<div class="main_content">';
    		  while ( have_rows('main_builder') ) {
    		    the_row();
    				if(get_row_layout() != "card_container_end"){
    					// get flexible content file based on layout name
    					locate_template( 'templates/builder/' . get_row_layout() . '.php', true, false );
    				}
    
    		  }
    			echo '</div>';
    		}
    	}
    
    	// if no posts
    	if ( ( !have_posts() ) || ( get_search_query() == ' ' ) ) {
    		basey_no_results();
    	}
    
    locate_template( 'templates/footer.php', true, true );
    

    As you can see I am looping through each row within the flexible content and then going out and grabbing a file that will then handle building out the layout with the information provided. Right now in each of those files at the very beginning I am setting all the variables I will need for said layout. This seems like a very inefficient way of doing things.

    My question is: Is there a way I get all the fields from the Flexible content field and then programmatically set it before the while loop so that I only call the information in once?

    Thanks for any help!

  • This does not directly answer your question, but WordPress has incredible caching for custom fields. You may see your query count climbing drastically while actual database hits are minimal because of all the cache hits.

    I would test your performance to see if this is an anticipated problem that does not actually need to be solved.

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

You must be logged in to reply to this topic.