Home › Forums › Add-ons › Options Page › Concern about ACF get_fields() efficiency… › Reply To: Concern about ACF get_fields() efficiency…
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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.