Make WordPress Core


Ignore:
Timestamp:
09/05/2025 01:28:20 PM (3 months ago)
Author:
SergeyBiryukov
Message:

Taxonomy: Add update_term_count action that fires when term counts are updated.

This allows plugins to run custom queries only when a term count is actually updated and not on every update of terms or posts.

Follow-up to [60365], [60510].

Props leonidasmilossis, peterwilsoncc, mukesh27, rollybueno, SergeyBiryukov.
Fixes #63904.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r60697 r60711  
    41764176    $post_statuses = esc_sql( apply_filters( 'update_post_term_count_statuses', $post_statuses, $taxonomy ) );
    41774177
    4178     foreach ( (array) $terms as $term ) {
     4178    foreach ( (array) $terms as $tt_id ) {
    41794179        $count = 0;
    41804180
     
    41824182        if ( $check_attachments ) {
    41834183            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
    4184             $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status IN ('" . implode( "', '", $post_statuses ) . "') OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) IN ('" . implode( "', '", $post_statuses ) . "') ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) );
     4184            $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status IN ('" . implode( "', '", $post_statuses ) . "') OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) IN ('" . implode( "', '", $post_statuses ) . "') ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $tt_id ) );
    41854185        }
    41864186
    41874187        if ( $object_types ) {
    41884188            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
    4189             $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status IN ('" . implode( "', '", $post_statuses ) . "') AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) );
    4190         }
     4189            $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status IN ('" . implode( "', '", $post_statuses ) . "') AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $tt_id ) );
     4190        }
     4191
     4192        /**
     4193         * Fires when a term count is calculated, before it is updated in the database.
     4194         *
     4195         * @since 6.9.0
     4196         *
     4197         * @param int    $tt_id         Term taxonomy ID.
     4198         * @param string $taxonomy_name Taxonomy slug.
     4199         * @param int    $count         Term count.
     4200         */
     4201        do_action( 'update_term_count', $tt_id, $taxonomy->name, $count );
    41914202
    41924203        /** This action is documented in wp-includes/taxonomy.php */
    4193         do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
    4194         $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
     4204        do_action( 'edit_term_taxonomy', $tt_id, $taxonomy->name );
     4205        $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $tt_id ) );
    41954206
    41964207        /** This action is documented in wp-includes/taxonomy.php */
    4197         do_action( 'edited_term_taxonomy', $term, $taxonomy->name );
     4208        do_action( 'edited_term_taxonomy', $tt_id, $taxonomy->name );
    41984209    }
    41994210}
     
    42164227    foreach ( (array) $terms as $term ) {
    42174228        $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
     4229
     4230        /** This action is documented in wp-includes/taxonomy.php */
     4231        do_action( 'update_term_count', $term, $taxonomy->name, $count );
    42184232
    42194233        /** This action is documented in wp-includes/taxonomy.php */
Note: See TracChangeset for help on using the changeset viewer.