From e0943855483f3f726b9460dc16fd6b1efb3f199e Mon Sep 17 00:00:00 2001 From: Rebecca Hum <16962021+rebeccahum@users.noreply.github.com> Date: Wed, 26 Jan 2022 13:24:37 -0700 Subject: [PATCH 1/2] Add helper get_total_objects_for_query_from_db() which counts directly from the DB --- includes/classes/Indexable/Post/Post.php | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index 27e7ba6a99..c5a54f4a93 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -189,9 +189,44 @@ private function get_total_objects_for_query( $query_args ) { $object_counts[ $cache_key ] = ( new WP_Query( $normalized_query_args ) )->found_posts; } + if ( 0 === $object_counts[ $cache_key ] ) { + // Do a DB count to make sure the query didn't just die and return 0. + $db_post_count = $this->get_total_objects_for_query_from_db( $normalized_query_args ); + + if ( $db_post_count !== $object_counts[ $cache_key ] ) { + $object_counts[ $cache_key ] = $db_post_count; + } + } + return $object_counts[ $cache_key ]; } + /** + * Get total posts from DB for a specific query based on it's args. + * + * @param array $query_args The query args. + * @return int The total posts. + */ + public function get_total_objects_for_query_from_db( $query_args ) { + $post_count = 0; + + if ( ! isset( $query_args['post_type'] ) ) { + return $post_count; + } + + foreach ( $query_args['post_type'] as $post_type ) { + $post_counts_by_post_status = wp_count_posts( $post_type ); + foreach ( $post_counts_by_post_status as $post_status => $post_status_count ) { + if ( ! in_array( $post_status, $query_args['post_status'] ) ) { + continue; + } + $post_count += $post_status_count; + } + } + + return $post_count; + } + /** * Returns indexable post types for the current site * From dc2d056bcb76406e1980ef509cc6bd552abdef86 Mon Sep 17 00:00:00 2001 From: Rebecca Hum <16962021+rebeccahum@users.noreply.github.com> Date: Thu, 27 Jan 2022 10:05:39 -0700 Subject: [PATCH 2/2] Address feedback --- includes/classes/Indexable/Post/Post.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index c5a54f4a93..e22a294041 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -168,7 +168,7 @@ public function bulk_indexing_filter_posts_where( $where, $query ) { * @param array $query_args The query args. * @return int The query result's found_posts. */ - private function get_total_objects_for_query( $query_args ) { + protected function get_total_objects_for_query( $query_args ) { static $object_counts = []; // Reset the pagination-related args for optimal caching. @@ -205,9 +205,10 @@ private function get_total_objects_for_query( $query_args ) { * Get total posts from DB for a specific query based on it's args. * * @param array $query_args The query args. + * @since 4.0.0 * @return int The total posts. */ - public function get_total_objects_for_query_from_db( $query_args ) { + protected function get_total_objects_for_query_from_db( $query_args ) { $post_count = 0; if ( ! isset( $query_args['post_type'] ) ) { @@ -217,7 +218,7 @@ public function get_total_objects_for_query_from_db( $query_args ) { foreach ( $query_args['post_type'] as $post_type ) { $post_counts_by_post_status = wp_count_posts( $post_type ); foreach ( $post_counts_by_post_status as $post_status => $post_status_count ) { - if ( ! in_array( $post_status, $query_args['post_status'] ) ) { + if ( ! in_array( $post_status, $query_args['post_status'], true ) ) { continue; } $post_count += $post_status_count;