It was suggested to use a transient method to remove the duplicate queries Duplicate Queries
Why does this don't work? Is it the wrong way to use it
private function get_acf_list()
{
if(!class_exists('acf')){
return array();
}
global $wpdb;
if ( false === ( $pgggo_qry = get_transient( 'pgggo_acf_query_results' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$pgggo_qry = "SELECT post_excerpt as 'field_name', post_title as 'field_label' FROM {$wpdb->prefix}posts where post_type = 'acf-field'";
set_transient( 'pgggo_acf_query_results', $pgggo_qry, 12 * 7200 );
}
$pgggo_list_results = $wpdb->get_results($pgggo_qry, ARRAY_A);
$acf_field_array = array();
if (!empty($pgggo_list_results)) {
foreach ($pgggo_list_results as $value) {
$acf_field_array[$value['field_name']] = $value['field_label'];
}
}
return $acf_field_array;
}
WP_Query
loop instead and WP will store these in memory viaWP_Cache
in the same request already. Caching plugins can then intercept and cache the query for you. You throw all those optimisations away by usingwpdb
directly – Tom J Nowell♦ Nov 10 at 23:28$pgggo_list_results
variable? There is n't any proper method to list all the ACF custom fields. If i use the WP_Query method I have to loop through each post type and posts to generate the fields.get_field_objects
in addition to that to retrive the post names. Which makes a heavy code than the SQL query. – Latheesh V M Villa Nov 11 at 10:06$special_query_results = new \WP_Query( 'post_excerpt=field_name&post_title=field_label&post_type=acf-field' );
– Latheesh V M Villa Nov 11 at 10:15