WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/14/2020 12:49:52 AM (14 months ago)
Author:
peterwilsoncc
Message:

Taxonomy: Improve performance of term recounting database queries.

When modifying terms assigned to an object, replace full term recounts with incrementing/decrementing the count as appropriate. This provides a significant performance boost on sites with a high number of term/object relationships and/or posts.

Introduces the functions wp_increment_term_count(), wp_decrement_term_count(), wp_modify_term_count_by() and wp_modify_term_count_by_now() for updating the term count.

Introduces the function _wp_prevent_term_counting() for preventing double counting on posts that are about to transition.

Adds the parameter update_count_by_callback to register_taxonomy() to allow developers to use a custom callback for incrementing or decrementing a term count.

Props boonebgorges, davidbaumwald, hellofromTonya, johnbillion, lcyh78, mattoperry, peterwilsoncc, rebasaurus, whyisjake.
Fixes #40351.

File:
1 edited

Legend:

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

    r48356 r49141  
    180180     */
    181181    public $update_count_callback;
     182
     183    /**
     184     * Function that will be called when the count is modified by an amount.
     185     *
     186     * @since 5.6.0
     187     * @var callable
     188     */
     189    public $update_count_by_callback;
    182190
    183191    /**
     
    278286
    279287        $defaults = array(
    280             'labels'                => array(),
    281             'description'           => '',
    282             'public'                => true,
    283             'publicly_queryable'    => null,
    284             'hierarchical'          => false,
    285             'show_ui'               => null,
    286             'show_in_menu'          => null,
    287             'show_in_nav_menus'     => null,
    288             'show_tagcloud'         => null,
    289             'show_in_quick_edit'    => null,
    290             'show_admin_column'     => false,
    291             'meta_box_cb'           => null,
    292             'meta_box_sanitize_cb'  => null,
    293             'capabilities'          => array(),
    294             'rewrite'               => true,
    295             'query_var'             => $this->name,
    296             'update_count_callback' => '',
    297             'show_in_rest'          => false,
    298             'rest_base'             => false,
    299             'rest_controller_class' => false,
    300             'default_term'          => null,
    301             '_builtin'              => false,
     288            'labels'                   => array(),
     289            'description'              => '',
     290            'public'                   => true,
     291            'publicly_queryable'       => null,
     292            'hierarchical'             => false,
     293            'show_ui'                  => null,
     294            'show_in_menu'             => null,
     295            'show_in_nav_menus'        => null,
     296            'show_tagcloud'            => null,
     297            'show_in_quick_edit'       => null,
     298            'show_admin_column'        => false,
     299            'meta_box_cb'              => null,
     300            'meta_box_sanitize_cb'     => null,
     301            'capabilities'             => array(),
     302            'rewrite'                  => true,
     303            'query_var'                => $this->name,
     304            'update_count_callback'    => '',
     305            'update_count_by_callback' => '',
     306            'show_in_rest'             => false,
     307            'rest_base'                => false,
     308            'rest_controller_class'    => false,
     309            'default_term'             => null,
     310            '_builtin'                 => false,
    302311        );
    303312
     
    412421        }
    413422
     423        // If generic update callback is defined but increment/decrement callback is not.
     424        if (
     425            ! empty( $args['update_count_callback'] ) &&
     426            is_callable( $args['update_count_callback'] ) &&
     427            empty( $args['update_count_by_callback'] )
     428        ) {
     429            $args['update_count_by_callback'] = function( $tt_ids, $taxonomy, $modify_by ) {
     430                return call_user_func( $args['update_count_callback'], $tt_ids, $taxonomy );
     431            };
     432        }
     433
    414434        foreach ( $args as $property_name => $property_value ) {
    415435            $this->$property_name = $property_value;
Note: See TracChangeset for help on using the changeset viewer.