Fix slow main RSS feed query

This commit is contained in:
Tetrakern 2023-11-12 12:42:25 +01:00
parent fac614bdf8
commit 5803437442
4 changed files with 37 additions and 38 deletions

View File

@ -93,8 +93,8 @@ if ( ! function_exists( 'fictioneer_get_card_list' ) ) {
$the_query_args['post__not_in'] = array_merge( $excluded, ( $the_query_args['post__not_in'] ?? [] ) );
}
// Query without excluded posts (results should already be cached)
$query = new WP_Query( $the_query_args );
// Query without excluded posts
$query = new WP_Query( $the_query_args );
// Prime author cache
if (

View File

@ -217,7 +217,7 @@ function fictioneer_remove_unlisted_from_search( $query ) {
);
}
}
add_action( 'pre_get_posts' ,'fictioneer_remove_unlisted_from_search', 10 );
add_action( 'pre_get_posts', 'fictioneer_remove_unlisted_from_search', 10 );
/**
* Extend search query with custom input

View File

@ -408,8 +408,8 @@ if ( ! function_exists( 'fictioneer_get_author_statistics' ) ) {
$stories = get_posts(
array(
'post_type' => array( 'fcn_story' ),
'post_status' => array( 'publish' ),
'post_type' => 'fcn_story',
'post_status' => 'publish',
'author' => $author_id,
'numberposts' => -1,
'orderby' => 'modified',
@ -431,8 +431,8 @@ if ( ! function_exists( 'fictioneer_get_author_statistics' ) ) {
$chapters = get_posts(
array(
'post_type' => array( 'fcn_chapter' ),
'post_status' => array( 'publish' ),
'post_type' => 'fcn_chapter',
'post_status' => 'publish',
'author' => $author_id,
'numberposts' => -1,
'orderby' => 'modified',

View File

@ -9,46 +9,44 @@
*/
// Query posts
$posts = new WP_Query(
array (
$posts = get_posts(
array(
'post_type' => ['post', 'fcn_story', 'fcn_chapter', 'fcn_recommendation'],
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => get_option( 'posts_per_rss' ),
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'fictioneer_chapter_hidden',
'value' => '0'
),
array(
'key' => 'fictioneer_chapter_hidden',
'compare' => 'NOT EXISTS'
)
),
array(
'relation' => 'OR',
array(
'key' => 'fictioneer_story_hidden',
'value' => '0'
),
array(
'key' => 'fictioneer_story_hidden',
'compare' => 'NOT EXISTS'
)
)
),
'posts_per_page' => get_option( 'posts_per_rss' ) + 8, // Buffer in case of invalid results
'update_post_meta_cache' => true,
'update_post_term_cache' => true,
'no_found_rows' => true
)
);
// Filter out hidden posts (faster than meta query)
$posts = array_filter( $posts, function ( $post ) {
// Chapter hidden?
if ( $post->post_type === 'fcn_chapter' ) {
$chapter_hidden = get_post_meta( $post->ID, 'fictioneer_chapter_hidden', true );
return empty( $chapter_hidden ) || $chapter_hidden === '0';
}
// Story hidden?
if ( $post->post_type === 'fcn_story' ) {
$story_hidden = get_post_meta( $post->ID, 'fictioneer_story_hidden', true );
return empty( $story_hidden ) || $story_hidden === '0';
}
// Keep
return true;
});
// Crop number of posts (remove buffer posts)
$posts = array_slice( $posts, 0, get_option( 'posts_per_rss' ) );
// Prime author cache
if ( function_exists( 'update_post_author_caches' ) ) {
update_post_author_caches( $posts->posts );
update_post_author_caches( $posts );
}
// Feed title
@ -116,9 +114,10 @@ do_action( 'rss_tag_pre', 'rss2' );
<?php endif; ?>
<?php
while ( $posts->have_posts() ) {
foreach ( $posts as $post ) {
// Setup
$posts->the_post();
// $posts->the_post();
setup_postdata( $post );
// Data
$pub_date = mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false );