I am trying to cache the javascript array generated by Views to be displayed on Google Maps. I'm generating the array from 1000+ posts and new posts are added only a few times per week. Thus the javascript array should be cached, not re-generated on every access. How would I go about doing this with Toolset/Views? Any advice would be appreciated.
Thanks.
Kris
Dear Kris,
Views is using wordpress build-in PHP class WP_Query to query the database, you can try other cache plugins, or use wordpress build-in cache feature:
More help:
http://codex.wordpress.org/Class_Reference/WP_Query
https://toolset.com/faq/working-with-caching-plugins/
https://codex.wordpress.org/WordPress_Optimization/Caching
Hi Luoy, yes, that's what I doing already post view-generation. The general site cache runs every 1 hour. What I need in this particular case though is to run the the view query only once per 24h. How would I achieve this? Thanks.
Kris
You can try wordpress build-in cache API Transients, for example:
1) create a custom shortcode in your theme/functions.php:
add_shortcode('cache-view-result', 'cache_view_result_func');
function cache_view_result_func($atts, $content){
extract( shortcode_atts( array(
'name' => '',
'expiration' => 24,
), $atts ));
$cache_name = 'cache_view_results_' . $name;
if ( false === ( $res = get_transient( $cache_name ) ) ) {
$res = render_view( array('name' => $name) );
set_transient( $cache_name, $res, $expiration * HOUR_IN_SECONDS );
}
return $res;
}
2) use above shortcode in your content, like this:
[cache-view-result name="my-view-slug" expiration=24]
More help:
Transients API
https://codex.wordpress.org/Transients_API