Skip to content

Commit

Permalink
In Terms Indexablequery_db, for term count, use wp_count_terms()
Browse files Browse the repository at this point in the history
…instead of `WP_Term_Query` to avoid `_prime_term_caches`

This is because on a DB with a lot of terms, calling `prime_term_caches` triggers an expensive, unnecessary query that isn't required for the ultimate term count. Also, add `hierarchical` => `false` for performance, otherwise it performs a no limit query
  • Loading branch information
rebeccahum committed Dec 13, 2023
1 parent afbb9a5 commit 60a1ca3
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions includes/classes/Indexable/Term/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,14 +620,14 @@ public function prepare_document( $term_id ) {
* @return array
*/
public function query_db( $args ) {

$defaults = [
'number' => $this->get_bulk_items_per_page(),
'offset' => 0,
'orderby' => 'id',
'order' => 'desc',
'taxonomy' => $this->get_indexable_taxonomies(),
'hide_empty' => false,
'number' => $this->get_bulk_items_per_page(),
'offset' => 0,
'orderby' => 'id',
'order' => 'desc',
'taxonomy' => $this->get_indexable_taxonomies(),
'hide_empty' => false,
'hierarchical' => false,
];

if ( isset( $args['per_page'] ) ) {
Expand All @@ -650,12 +650,6 @@ public function query_db( $args ) {
unset( $all_query_args['offset'] );
unset( $all_query_args['fields'] );

/**
* This just seems so inefficient.
*
* @todo Better way to do this?
*/

/**
* Filter database arguments for term count query
*
Expand All @@ -664,9 +658,8 @@ public function query_db( $args ) {
* @since 3.4
* @return {array} New arguments
*/
$all_query = new WP_Term_Query( apply_filters( 'ep_term_all_query_db_args', $all_query_args, $args ) );

$total_objects = count( $all_query->terms );
$total_objects = wp_count_terms( apply_filters( 'ep_term_all_query_db_args', $all_query_args, $args ) );
$total_objects = ! is_wp_error( $total_objects ) ? (int) $total_objects : 0;

if ( ! empty( $args['offset'] ) ) {
if ( (int) $args['offset'] >= $total_objects ) {
Expand Down

0 comments on commit 60a1ca3

Please sign in to comment.