Skip to content

Commit dfc5c2f

Browse files
committed
Add some hooks to cache the queries and retrieve them.
1 parent a9c7da4 commit dfc5c2f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

includes/query-loop.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,43 @@ function add_custom_query_params( $args, $request ) {
163163

164164
return $merged;
165165
}
166+
167+
168+
169+
/**
170+
* Retrieve any cached AQL instances and bypass making a query.
171+
*/
172+
add_filter(
173+
'posts_pre_query',
174+
function ( $null_return, $query ) {
175+
if ( isset( $query->query['is_aql'] ) ) {
176+
$cached_query = get_transient( $query->query_vars_hash );
177+
if ( $cached_query ) {
178+
$query->found_posts = $cached_query->found_posts;
179+
$query->max_num_pages = $cached_query->max_num_pages;
180+
return $cached_query->posts;
181+
}
182+
return $null_return;
183+
}
184+
return $null_return;
185+
},
186+
10,
187+
2
188+
);
189+
190+
/**
191+
* Create a cache for the Query generated by the AQL instance.
192+
*/
193+
add_filter(
194+
'the_posts',
195+
function ( $posts, $query ) {
196+
if ( isset( $query->query['is_aql'] ) ) {
197+
if ( ! get_transient( $query->query_vars_hash ) ) {
198+
set_transient( $query->query_vars_hash, $query, HOUR_IN_SECONDS );
199+
}
200+
}
201+
return $posts;
202+
},
203+
10,
204+
2
205+
);

0 commit comments

Comments
 (0)