Add Transient cache to index page templates

This commit is contained in:
Tetrakern 2024-09-02 02:05:05 +02:00
parent bbc7695e2e
commit 38dc6da10c
4 changed files with 194 additions and 134 deletions

View File

@ -10,16 +10,25 @@
// Setup
$post_id = get_the_ID();
$sorted_authors = [];
$cached = fictioneer_caching_active( 'author_index_advanced' );
// Header
get_header();
// Transient cache?
$transient = $cached ? 0 : get_transient( 'fictioneer_author_index_advanced_' . $post_id );
if ( $transient ) {
$sorted_authors = $transient;
}
// ... if not cached
if ( empty( $sorted_authors ) ) {
// Query authors with published posts
$authors = fictioneer_get_publishing_authors( array( 'fields' => array( 'ID', 'display_name', 'user_nicename' ) ) );
// Sort authors
$sorted_authors = [];
if ( ! empty( $authors ) ) {
// Loop through authors...
foreach ( $authors as $author ) {
@ -47,6 +56,12 @@ if ( ! empty( $authors ) ) {
// Sort by index
ksort( $sorted_authors );
// Cache as Transient
if ( ! $cached ) {
set_transient( 'fictioneer_author_index_advanced_' . $post_id, $sorted_authors, 12 * HOUR_IN_SECONDS );
}
}
}
// Last key

View File

@ -10,16 +10,25 @@
// Setup
$post_id = get_the_ID();
$sorted_authors = [];
$cached = fictioneer_caching_active( 'author_index' );
// Header
get_header();
// Transient cache?
$transient = $cached ? 0 : get_transient( 'fictioneer_author_index_' . $post_id );
if ( $transient ) {
$sorted_authors = $transient;
}
// ... if not cached
if ( empty( $sorted_authors ) ) {
// Query authors with published posts
$authors = fictioneer_get_publishing_authors( array( 'fields' => array( 'ID', 'display_name', 'user_nicename' ) ) );
// Sort authors
$sorted_authors = [];
if ( ! empty( $authors ) ) {
// Loop through authors...
foreach ( $authors as $author ) {
@ -45,6 +54,12 @@ if ( ! empty( $authors ) ) {
// Sort by index
ksort( $sorted_authors );
// Cache as Transient
if ( ! $cached ) {
set_transient( 'fictioneer_author_index_' . $post_id, $sorted_authors, 12 * HOUR_IN_SECONDS );
}
}
}
// Last key

View File

@ -10,10 +10,21 @@
// Setup
$post_id = get_the_ID();
$sorted_stories = [];
$cached = fictioneer_caching_active( 'story_index_advanced' );
// Header
get_header();
// Transient cache?
$transient = $cached ? 0 : get_transient( 'fictioneer_story_index_advanced_' . $post_id );
if ( $transient ) {
$sorted_stories = $transient;
}
// ... if not cached
if ( empty( $sorted_stories ) ) {
// Query all stories
$args = array(
'post_type' => 'fcn_story',
@ -28,8 +39,6 @@ $args = array(
$stories = new WP_Query( $args );
// Sort stories
$sorted_stories = [];
if ( $stories->have_posts() ) {
// Loop through posts...
foreach ( $stories->posts as $story ) {
@ -67,6 +76,12 @@ if ( $stories->have_posts() ) {
// Sort by index
ksort( $sorted_stories );
// Cache as Transient
if ( ! $cached ) {
set_transient( 'fictioneer_story_index_advanced_' . $post_id, $sorted_stories, 12 * HOUR_IN_SECONDS );
}
}
}
// Last key

View File

@ -10,10 +10,21 @@
// Setup
$post_id = get_the_ID();
$sorted_stories = [];
$cached = fictioneer_caching_active( 'story_index' );
// Header
get_header();
// Transient cache?
$transient = $cached ? 0 : get_transient( 'fictioneer_story_index_' . $post_id );
if ( $transient ) {
$sorted_stories = $transient;
}
// ... if not cached
if ( empty( $sorted_stories ) ) {
// Query all stories
$args = array(
'post_type' => 'fcn_story',
@ -28,8 +39,6 @@ $args = array(
$stories = new WP_Query( $args );
// Sort stories
$sorted_stories = [];
if ( $stories->have_posts() ) {
// Loop through posts...
foreach ( $stories->posts as $story ) {
@ -63,6 +72,12 @@ if ( $stories->have_posts() ) {
// Sort by index
ksort( $sorted_stories );
// Cache as Transient
if ( ! $cached ) {
set_transient( 'fictioneer_story_index_' . $post_id, $sorted_stories, 12 * HOUR_IN_SECONDS );
}
}
}
// Last key