一般来说,您可以使用WP Transients API 到save the query.
// Get any existing copy of our transient data
if ( false === ( $special_query_results = get_transient( \'special_query_results\' ) ) ) {
// It wasn\'t there, so regenerate the data and save the transient
$special_query_results = new WP_Query( \'cat=5&order=random&tag=tech&post_meta_key=thumbnail\' );
set_transient( \'special_query_results\', $special_query_results, 12 * HOUR_IN_SECONDS );
}
// Use the data like you would have normally...
This article 通过论证存储
WP_Query()
对象作为瞬态,并建议将昂贵查询返回的post ID存储在瞬态中,然后使用这些ID创建新的
WP_Query
. 诚然,使用这种方法,我们将很快返回到重复查询,但它们将是更轻的查询。
$cache_key = \'my-expensive-query\';
if ( ! $ids = get_transient( $cache_key ) ) {
$query = new WP_Query( array(
\'fields\' => \'ids\',
// ...
) );
$ids = $query->posts;
set_transient( $cache_key, $ids, 24 * HOUR_IN_SECONDS );
}
$query = new WP_Query( array(
\'post__in\' => $ids,
) );
// while ( $query->have_posts() ) ...