Support

Account

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

  • 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!