Support

Account

Home Forums Add-ons Options Page Concern about ACF get_fields() efficiency…

Solved

Concern about ACF get_fields() efficiency…

  • For testing purposes I have edited page.php to have nothing other than:

    <?php while ( have_posts() ) : the_post(); ?>
    <h1><?php the_title(); ?></h1>
    <?php $site_options = get_fields('options'); ?>
    <?php endwhile; ?>

    I removed get_header, get_footer and only have ACF plugins enabled because I am still early in development and doing some initial testing. When I view the page without calling get_fields it reports 9 queries but if I visit the page with get_fields it jumps to 72 queries. It seems like that should only bump by 1 or 2 queries…not 63.

    Queries are reported using:

    get_num_queries();

    Should I be concerned about this?

  • Results from get_options call:
    
        Array(
        [main_ad_-_image] => Array
            (
                [id] => 25
                [alt] => TEST
                [title] => lg-social_media
                [caption] => 
                [description] => 
                [mime_type] => image/png
                [url] => {urlhere}
                [width] => 460
                [height] => 260
                [sizes] => Array
                    (
                        [thumbnail] => {urlhere}/wp-content/uploads/lg-social_media-150x150.png
                        [thumbnail-width] => 150
                        [thumbnail-height] => 150
                        [medium] => {urlhere}/wp-content/uploads/lg-social_media-220x124.png
                        [medium-width] => 220
                        [medium-height] => 124
                        [large] => {urlhere}/wp-content/uploads/lg-social_media.png
                        [large-width] => 460
                        [large-height] => 260
                    )
    
            )
    
        [main_ad_-_url_type] => ext
        [main_ad_-_url] => www.google.com
        [small_ad_1_-_image] => Array
            (
                [id] => 28
                [alt] => Fake Text
                [title] => fake-text
                [caption] => 
                [description] => 
                [mime_type] => image/png
                [url] => {urlhere}/wp-content/uploads/fake-text.png
                [width] => 220
                [height] => 120
                [sizes] => Array
                    (
                        [thumbnail] => {urlhere}/wp-content/uploads/sm-fake-text-150x120.png
                        [thumbnail-width] => 150
                        [thumbnail-height] => 120
                        [medium] => {urlhere}/wp-content/uploads/sm-fake-text.png
                        [medium-width] => 220
                        [medium-height] => 120
                        [large] => {urlhere}/wp-content/uploads/sm-fake-text.png
                        [large-width] => 220
                        [large-height] => 120
                    )
    
            )
    
        [small_ad_1_-_url_type] => ext
        [small_ad_2_-_image] => Array
            (
                [id] => 30
                [alt] => Fake Text 2
                [title] => fake-text2
                [caption] => 
                [description] => 
                [mime_type] => image/png
                [url] => {urlhere}/wp-content/uploads/fake-text2.png
                [width] => 220
                [height] => 120
                [sizes] => Array
                    (
                        [thumbnail] => {urlhere}/wp-content/uploads/fake-text2-150x120.png
                        [thumbnail-width] => 150
                        [thumbnail-height] => 120
                        [medium] => {urlhere}/wp-content/uploads/fake-text2.png
                        [medium-width] => 220
                        [medium-height] => 120
                        [large] => {urlhere}/wp-content/uploads/fake-text2.png
                        [large-width] => 220
                        [large-height] => 120
                    )
    
            )
    
        [small_ad_1_-_url] => www.google.com
        [main_ad_-_page] => {urlhere}
        [small_ad_2_-_url_type] => ext
        [small_ad_2_-_url] => www.google.com
        [small_ad_3_-_image] => Array
            (
                [id] => 27
                [alt] => Fake Text 3
                [title] => fake-text3
                [caption] => 
                [description] => 
                [mime_type] => image/png
                [url] => {urlhere}/wp-content/uploads/fake-text3.png
                [width] => 220
                [height] => 120
                [sizes] => Array
                    (
                        [thumbnail] => {urlhere}/wp-content/uploads/fake-text3-150x120.png
                        [thumbnail-width] => 150
                        [thumbnail-height] => 120
                        [medium] => {urlhere}/wp-content/uploads/fake-text3.png
                        [medium-width] => 220
                        [medium-height] => 120
                        [large] => {urlhere}/wp-content/uploads/fake-text3.png
                        [large-width] => 220
                        [large-height] => 120
                    )
    
            )
    
        [small_ad_3_-_url_type] => ext
        [small_ad_3_-_url] => www.google.com
        [small_ad_4_-_image] => Array
            (
                [id] => 29
                [alt] => Fake Text 4
                [title] => fake-text4
                [caption] => 
                [description] => 
                [mime_type] => image/png
                [url] => {urlhere}/wp-content/uploads/fake-text4.png
                [width] => 220
                [height] => 120
                [sizes] => Array
                    (
                        [thumbnail] => {urlhere}/wp-content/uploads/fake-text4-150x120.png
                        [thumbnail-width] => 150
                        [thumbnail-height] => 120
                        [medium] => {urlhere}/wp-content/uploads/fake-text4.png
                        [medium-width] => 220
                        [medium-height] => 120
                        [large] => {urlhere}/wp-content/uploads/fake-text4.png
                        [large-width] => 220
                        [large-height] => 120
                    )
    
            )
    
        [small_ad_4_-_url_type] => ext
        [small_ad_4_-_url] => www.google.com
        [facebook] => www.facebook.com
        [twitter] => twitter.com
        [youtube] => www.youtube.com
        [office_address] => text
        [office_address_-_google_url] => test
        [office_-_phone] => 1234567890
        [office_-_800_#] => 8001234567
        [office_-_email] => email@email.com)
        
    
  • Hi @adjteam

    The get_fields function is in no means efficient. It will load the maximum amount of data possible.

    All your fields are images, so your website is loading in all the image sizes and metadata for each image.

    Do you need to be worried? Maybe. Perhaps you should load in only the data you need, using get_field when needed, and maybe think about loading in the image ID (image field setting) and loading only the image data you need.

    Thanks
    E

  • There might be some reason this is a bad idea, but what we’ve done when needing a big data pull is something like below. Performance is much better than get_fields or looping through many records with many get_field() calls inside.

    Maybe a start of a concept that could be incorporated into the plugin somehow.

    // - PEOPLE
    function get_people() {
    	
    	$people = array(); $person_fields = ''; $person_slug = 'person';
    	
    	// Fields
    	$fields = array('first_name', 'middle_name', 'last_name', 'suffix', 'email', 'phone', 'fax', 'linkedin', 'twitter', 'facebook', 'image', 'excerpt');
    		
    	// Build pull fields
    	foreach ($fields as $f) {
    		$person_fields .= "
    		MAX( CASE WHEN wp_postmeta.meta_key = '".$f."'
    		THEN wp_postmeta.meta_value
    		END ) AS '".$f."',";
    	}
    	
    	// Query
    	$sql = "SELECT * FROM (
    		SELECT 
    			wp_posts.ID, 
    			wp_posts.menu_order, 
    			wp_posts.post_title AS 'post_title',
    			wp_posts.post_name AS 'post_name',
    			".$person_fields."
    			concat('".get_bloginfo('url')."/".$person_slug."/',wp_posts.post_name,'/') AS 'permalink'
    		FROM wp_posts
    		LEFT JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
    		WHERE wp_posts.post_status = 'publish'
    		AND wp_posts.post_type = 'person'
    		GROUP BY wp_posts.ID
    		ORDER BY menu_order asc, last_name asc, first_name asc		
    	) AS t WHERE 1=1";
    	$results = $wpdb->get_results($sql,'ARRAY_A'); 
    
    	// Loop
    	foreach ($results as $r) {
    
    		// Something Special
    		$full_name = $r['first_name'];
    		if (!empty($r['middle_name'])) { $full_name .= " " . trim($r['middle_name']); }
    		if (!empty($r['last_name'])) { $full_name .= " " . trim($r['last_name']); }
    		if (!empty($r['suffix'])) { $full_name .= ", " . trim($r['suffix']); }
    		$r['full_name'] = trim($full_name);	
    				
    		// Package
    		$people[$r['ID']] = $r;
    	}
    	
    	return $people;
    }
    

    Hope it helps!

  • Another concept we’ve used actually is a bit of an internal caching system. Something like…

    // Save People + Build cached json data
    function my_save_people( $post_id ) {
    	if ( get_post_type($post_id) == 'person') {
    	
    		// Get all and pack data
    		$data = array();
    		$args = array( 'post_type' => 'person', 'posts_per_page' => -1, 'post_status' => 'publish' );
    		$the_query = new WP_Query( $args );
    		if ( $the_query->have_posts() ) {
    			while ( $the_query->have_posts() ) {
    				$the_query->the_post();
    				$array = get_fields(get_the_ID());
    				$array['name'] = get_the_title();
    				$array['permalink'] = get_the_permalink();
    				$data[] = $array;
    			}
    		}
    		wp_reset_postdata();	
    
    		// Save to cache
    		my_save_cache('people.json', $data);
    	}
    }
    add_action('acf/save_post', 'my_save_people', 20);
    
    // Save Cache
    function my_save_cache($file, $data) {
    	file_put_contents(SOME_PATH . '/cache/' . $file, json_encode($data));
    }
    
    // Get Cache item
    function my_get_cache($file) {
    	return json_decode(file_get_contents(SOME_PATH . '/cache/' . $file));
    }
    

    This puts most of the processing on the admin user when a ‘person’ is added/edited, then writes all the data to a text file which can then be pulled back in as a PHP array. No db hits at all this way for regular visitors.

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

The topic ‘Concern about ACF get_fields() efficiency…’ is closed to new replies.