Skip to content

Instantly share code, notes, and snippets.

@jeffrey-hearn
Last active December 14, 2015 07:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeffrey-hearn/5053954 to your computer and use it in GitHub Desktop.
Save jeffrey-hearn/5053954 to your computer and use it in GitHub Desktop.
Incomplete example of how one would cache the default WordPress query so that the database is never hit.
<?php
add_filter( 'posts_request', 'pre_db_hit' );
function pre_db_hit( $request )
{
global $diy_cache_current_request;
$diy_cache_current_request = $request;
// use request string to see if expired...
if ( $diy_cache_expired )
return $request; // Let query happen
return null; // null request shuts down execution of the query
}
add_filter( 'posts_results', 'post_db_hit' );
function post_db_hit( $post_objects )
{
global $diy_cache_current_request;
// use request string to see if expired...
if ( $diy_cache_expired )
{
// Store the post objects in the cache
// under a key consisting of a hash of
// the request string...
} else {
// Get the post objects from your DIY
// cache and return them...
return $cached_post_objects;
}
return $post_objects;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment