Fix slow query for author chapters

This commit is contained in:
Tetrakern 2023-11-12 01:14:34 +01:00
parent 90f42d18ab
commit 426ae29f10
3 changed files with 25 additions and 29 deletions

View File

@ -70,32 +70,7 @@ $tabs['chapters'] = array(
'post_type' => 'fcn_chapter',
'author' => $author_id,
'paged' => $current_page,
'order' => $order,
'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_chapter_no_chapter',
'value' => '0'
),
array(
'key' => 'fictioneer_chapter_no_chapter',
'compare' => 'NOT EXISTS'
)
)
)
'order' => $order
),
'classes' => [],
'empty' => __( 'No chapters published yet.', 'fictioneer' )

View File

@ -59,7 +59,8 @@ if ( ! function_exists( 'fictioneer_get_card_list' ) ) {
'orderby' => 'modified',
'order' => 'DESC',
'posts_per_page' => get_option( 'posts_per_page' ),
'no_found_rows' => $query_args['no_found_rows'] ?? false
'no_found_rows' => $query_args['no_found_rows'] ?? false,
'update_post_meta_cache' => true
);
// Default card arguments
@ -73,7 +74,27 @@ if ( ! function_exists( 'fictioneer_get_card_list' ) ) {
// Query (but not if 'post__in' is set and empty)
if ( ! ( isset( $the_query_args['post__in'] ) && empty( $the_query_args['post__in'] ) ) ) {
$query = new WP_Query( $the_query_args );
$all_query = new WP_Query( $the_query_args );
// Get excluded posts (faster than meta query)
$excluded = [];
foreach ( $all_query->posts as $candidate ) {
if (
fictioneer_get_field( 'fictioneer_chapter_hidden', $candidate->ID ) ||
fictioneer_get_field( 'fictioneer_chapter_no_chapter', $candidate->ID ) ||
fictioneer_get_field( 'fictioneer_story_hidden', $candidate->ID )
) {
$excluded[] = $candidate->ID;
}
}
if ( ! empty( $excluded ) ) {
$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 );
// Prime author cache
if (

View File

@ -86,7 +86,7 @@ if ( ! function_exists( 'fictioneer_build_chapters_schema' ) ) {
$schema = fictioneer_get_schema_node_root();
$image_data = fictioneer_get_schema_primary_image( $post_id );
// Filter out invalid chapters (10x faster than meta query)
// Filter out invalid chapters (faster than meta query)
$list = array_filter( $list_candidates, function ( $post_id ) {
// Chapter hidden?
$chapter_hidden = get_post_meta( $post_id, 'fictioneer_chapter_hidden', true );