_update_post_term_count

The timeline below displays how wordpress function _update_post_term_count has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

//
// Default callbacks.
//
/**
 * Updates term count based on object types of the current taxonomy.
 *
 * Private function for the default callback for post_tag and category
 * taxonomies.
 *
 * @access private
 * @since 2.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int[]       $terms    List of term taxonomy IDs.
 * @param WP_Taxonomy $taxonomy Current taxonomy object of terms.
 */
function _update_post_term_count($terms, $taxonomy)
{
    global $wpdb;
    $object_types = (array) $taxonomy->object_type;
    foreach ($object_types as &$object_type) {
        list($object_type) = explode(':', $object_type);
    }
    $object_types = array_unique($object_types);
    $check_attachments = array_search('attachment', $object_types, true);
    if (false !== $check_attachments) {
        unset($object_types[$check_attachments]);
        $check_attachments = true;
    }
    if ($object_types) {
        $object_types = esc_sql(array_filter($object_types, 'post_type_exists'));
    }
    $post_statuses = array('publish');
    /**
     * Filters the post statuses for updating the term count.
     *
     * @since 5.7.0
     *
     * @param string[]    $post_statuses List of post statuses to include in the count. Default is 'publish'.
     * @param WP_Taxonomy $taxonomy      Current taxonomy object.
     */
    $post_statuses = esc_sql(apply_filters('update_post_term_count_statuses', $post_statuses, $taxonomy));
    foreach ((array) $terms as $term) {
        $count = 0;
        // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
        if ($check_attachments) {
            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
            $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));
        }
        if ($object_types) {
            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
            $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));
        }
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edit_term_taxonomy', $term, $taxonomy->name);
        $wpdb->update($wpdb->term_taxonomy, compact('count'), array('term_taxonomy_id' => $term));
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edited_term_taxonomy', $term, $taxonomy->name);
    }
}

WordPress Version: 5.7

//
// Default callbacks.
//
/**
 * Will update term count based on object types of the current taxonomy.
 *
 * Private function for the default callback for post_tag and category
 * taxonomies.
 *
 * @access private
 * @since 2.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int[]       $terms    List of Term taxonomy IDs.
 * @param WP_Taxonomy $taxonomy Current taxonomy object of terms.
 */
function _update_post_term_count($terms, $taxonomy)
{
    global $wpdb;
    $object_types = (array) $taxonomy->object_type;
    foreach ($object_types as &$object_type) {
        list($object_type) = explode(':', $object_type);
    }
    $object_types = array_unique($object_types);
    $check_attachments = array_search('attachment', $object_types, true);
    if (false !== $check_attachments) {
        unset($object_types[$check_attachments]);
        $check_attachments = true;
    }
    if ($object_types) {
        $object_types = esc_sql(array_filter($object_types, 'post_type_exists'));
    }
    $post_statuses = array('publish');
    /**
     * Filters the post statuses for updating the term count.
     *
     * @since 5.7.0
     *
     * @param string[]    $post_statuses List of post statuses to include in the count. Default is 'publish'.
     * @param WP_Taxonomy $taxonomy      Current taxonomy object.
     */
    $post_statuses = esc_sql(apply_filters('update_post_term_count_statuses', $post_statuses, $taxonomy));
    foreach ((array) $terms as $term) {
        $count = 0;
        // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
        if ($check_attachments) {
            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
            $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));
        }
        if ($object_types) {
            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
            $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));
        }
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edit_term_taxonomy', $term, $taxonomy->name);
        $wpdb->update($wpdb->term_taxonomy, compact('count'), array('term_taxonomy_id' => $term));
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edited_term_taxonomy', $term, $taxonomy->name);
    }
}

WordPress Version: 5.5

//
// Default callbacks.
//
/**
 * Will update term count based on object types of the current taxonomy.
 *
 * Private function for the default callback for post_tag and category
 * taxonomies.
 *
 * @access private
 * @since 2.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int[]       $terms    List of Term taxonomy IDs.
 * @param WP_Taxonomy $taxonomy Current taxonomy object of terms.
 */
function _update_post_term_count($terms, $taxonomy)
{
    global $wpdb;
    $object_types = (array) $taxonomy->object_type;
    foreach ($object_types as &$object_type) {
        list($object_type) = explode(':', $object_type);
    }
    $object_types = array_unique($object_types);
    $check_attachments = array_search('attachment', $object_types, true);
    if (false !== $check_attachments) {
        unset($object_types[$check_attachments]);
        $check_attachments = true;
    }
    if ($object_types) {
        $object_types = esc_sql(array_filter($object_types, 'post_type_exists'));
    }
    foreach ((array) $terms as $term) {
        $count = 0;
        // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
        if ($check_attachments) {
            $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 = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM {$wpdb->posts} WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term));
        }
        if ($object_types) {
            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
            $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 = 'publish' AND post_type IN ('" . implode("', '", $object_types) . "') AND term_taxonomy_id = %d", $term));
        }
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edit_term_taxonomy', $term, $taxonomy->name);
        $wpdb->update($wpdb->term_taxonomy, compact('count'), array('term_taxonomy_id' => $term));
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edited_term_taxonomy', $term, $taxonomy->name);
    }
}

WordPress Version: 5.4

//
// Default callbacks.
//
/**
 * Will update term count based on object types of the current taxonomy.
 *
 * Private function for the default callback for post_tag and category
 * taxonomies.
 *
 * @access private
 * @since 2.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int[]       $terms    List of Term taxonomy IDs.
 * @param WP_Taxonomy $taxonomy Current taxonomy object of terms.
 */
function _update_post_term_count($terms, $taxonomy)
{
    global $wpdb;
    $object_types = (array) $taxonomy->object_type;
    foreach ($object_types as &$object_type) {
        list($object_type) = explode(':', $object_type);
    }
    $object_types = array_unique($object_types);
    $check_attachments = array_search('attachment', $object_types);
    if (false !== $check_attachments) {
        unset($object_types[$check_attachments]);
        $check_attachments = true;
    }
    if ($object_types) {
        $object_types = esc_sql(array_filter($object_types, 'post_type_exists'));
    }
    foreach ((array) $terms as $term) {
        $count = 0;
        // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
        if ($check_attachments) {
            $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 = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM {$wpdb->posts} WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term));
        }
        if ($object_types) {
            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
            $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 = 'publish' AND post_type IN ('" . implode("', '", $object_types) . "') AND term_taxonomy_id = %d", $term));
        }
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edit_term_taxonomy', $term, $taxonomy->name);
        $wpdb->update($wpdb->term_taxonomy, compact('count'), array('term_taxonomy_id' => $term));
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edited_term_taxonomy', $term, $taxonomy->name);
    }
}

WordPress Version: 5.3

//
// Default callbacks
//
/**
 * Will update term count based on object types of the current taxonomy.
 *
 * Private function for the default callback for post_tag and category
 * taxonomies.
 *
 * @access private
 * @since 2.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param array  $terms    List of Term taxonomy IDs.
 * @param object $taxonomy Current taxonomy object of terms.
 */
function _update_post_term_count($terms, $taxonomy)
{
    global $wpdb;
    $object_types = (array) $taxonomy->object_type;
    foreach ($object_types as &$object_type) {
        list($object_type) = explode(':', $object_type);
    }
    $object_types = array_unique($object_types);
    $check_attachments = array_search('attachment', $object_types);
    if (false !== $check_attachments) {
        unset($object_types[$check_attachments]);
        $check_attachments = true;
    }
    if ($object_types) {
        $object_types = esc_sql(array_filter($object_types, 'post_type_exists'));
    }
    foreach ((array) $terms as $term) {
        $count = 0;
        // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
        if ($check_attachments) {
            $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 = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM {$wpdb->posts} WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term));
        }
        if ($object_types) {
            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
            $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 = 'publish' AND post_type IN ('" . implode("', '", $object_types) . "') AND term_taxonomy_id = %d", $term));
        }
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edit_term_taxonomy', $term, $taxonomy->name);
        $wpdb->update($wpdb->term_taxonomy, compact('count'), array('term_taxonomy_id' => $term));
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edited_term_taxonomy', $term, $taxonomy->name);
    }
}

WordPress Version: 4.3

//
// Default callbacks
//
/**
 * Will update term count based on object types of the current taxonomy.
 *
 * Private function for the default callback for post_tag and category
 * taxonomies.
 *
 * @access private
 * @since 2.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param array  $terms    List of Term taxonomy IDs.
 * @param object $taxonomy Current taxonomy object of terms.
 */
function _update_post_term_count($terms, $taxonomy)
{
    global $wpdb;
    $object_types = (array) $taxonomy->object_type;
    foreach ($object_types as &$object_type) {
        list($object_type) = explode(':', $object_type);
    }
    $object_types = array_unique($object_types);
    if (false !== $check_attachments = array_search('attachment', $object_types)) {
        unset($object_types[$check_attachments]);
        $check_attachments = true;
    }
    if ($object_types) {
        $object_types = esc_sql(array_filter($object_types, 'post_type_exists'));
    }
    foreach ((array) $terms as $term) {
        $count = 0;
        // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
        if ($check_attachments) {
            $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 = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM {$wpdb->posts} WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term));
        }
        if ($object_types) {
            $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 = 'publish' AND post_type IN ('" . implode("', '", $object_types) . "') AND term_taxonomy_id = %d", $term));
        }
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edit_term_taxonomy', $term, $taxonomy->name);
        $wpdb->update($wpdb->term_taxonomy, compact('count'), array('term_taxonomy_id' => $term));
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edited_term_taxonomy', $term, $taxonomy->name);
    }
}

WordPress Version: 4.2

//
// Default callbacks
//
/**
 * Will update term count based on object types of the current taxonomy.
 *
 * Private function for the default callback for post_tag and category
 * taxonomies.
 *
 * @access private
 * @since 2.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param array $terms List of Term taxonomy IDs
 * @param object $taxonomy Current taxonomy object of terms
 */
function _update_post_term_count($terms, $taxonomy)
{
    global $wpdb;
    $object_types = (array) $taxonomy->object_type;
    foreach ($object_types as &$object_type) {
        list($object_type) = explode(':', $object_type);
    }
    $object_types = array_unique($object_types);
    if (false !== $check_attachments = array_search('attachment', $object_types)) {
        unset($object_types[$check_attachments]);
        $check_attachments = true;
    }
    if ($object_types) {
        $object_types = esc_sql(array_filter($object_types, 'post_type_exists'));
    }
    foreach ((array) $terms as $term) {
        $count = 0;
        // Attachments can be 'inherit' status, we need to base count off the parent's status if so
        if ($check_attachments) {
            $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 = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM {$wpdb->posts} WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term));
        }
        if ($object_types) {
            $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 = 'publish' AND post_type IN ('" . implode("', '", $object_types) . "') AND term_taxonomy_id = %d", $term));
        }
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edit_term_taxonomy', $term, $taxonomy->name);
        $wpdb->update($wpdb->term_taxonomy, compact('count'), array('term_taxonomy_id' => $term));
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edited_term_taxonomy', $term, $taxonomy->name);
    }
}

WordPress Version: 4.1

//
// Default callbacks
//
/**
 * Will update term count based on object types of the current taxonomy.
 *
 * Private function for the default callback for post_tag and category
 * taxonomies.
 *
 * @access private
 * @since 2.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param array $terms List of Term taxonomy IDs
 * @param object $taxonomy Current taxonomy object of terms
 */
function _update_post_term_count($terms, $taxonomy)
{
    global $wpdb;
    $object_types = (array) $taxonomy->object_type;
    foreach ($object_types as &$object_type) {
        list($object_type) = explode(':', $object_type);
    }
    $object_types = array_unique($object_types);
    if (false !== $check_attachments = array_search('attachment', $object_types)) {
        unset($object_types[$check_attachments]);
        $check_attachments = true;
    }
    if ($object_types) {
        $object_types = esc_sql(array_filter($object_types, 'post_type_exists'));
    }
    foreach ((array) $terms as $term) {
        $count = 0;
        // Attachments can be 'inherit' status, we need to base count off the parent's status if so
        if ($check_attachments) {
            $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 = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM {$wpdb->posts} WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term));
        }
        if ($object_types) {
            $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 = 'publish' AND post_type IN ('" . implode("', '", $object_types) . "') AND term_taxonomy_id = %d", $term));
        }
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edit_term_taxonomy', $term, $taxonomy);
        $wpdb->update($wpdb->term_taxonomy, compact('count'), array('term_taxonomy_id' => $term));
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edited_term_taxonomy', $term, $taxonomy);
    }
}

WordPress Version: 3.9

//
// Default callbacks
//
/**
 * Will update term count based on object types of the current taxonomy.
 *
 * Private function for the default callback for post_tag and category
 * taxonomies.
 *
 * @access private
 * @since 2.3.0
 * @uses $wpdb
 *
 * @param array $terms List of Term taxonomy IDs
 * @param object $taxonomy Current taxonomy object of terms
 */
function _update_post_term_count($terms, $taxonomy)
{
    global $wpdb;
    $object_types = (array) $taxonomy->object_type;
    foreach ($object_types as &$object_type) {
        list($object_type) = explode(':', $object_type);
    }
    $object_types = array_unique($object_types);
    if (false !== $check_attachments = array_search('attachment', $object_types)) {
        unset($object_types[$check_attachments]);
        $check_attachments = true;
    }
    if ($object_types) {
        $object_types = esc_sql(array_filter($object_types, 'post_type_exists'));
    }
    foreach ((array) $terms as $term) {
        $count = 0;
        // Attachments can be 'inherit' status, we need to base count off the parent's status if so
        if ($check_attachments) {
            $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 = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM {$wpdb->posts} WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term));
        }
        if ($object_types) {
            $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 = 'publish' AND post_type IN ('" . implode("', '", $object_types) . "') AND term_taxonomy_id = %d", $term));
        }
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edit_term_taxonomy', $term, $taxonomy);
        $wpdb->update($wpdb->term_taxonomy, compact('count'), array('term_taxonomy_id' => $term));
        /** This action is documented in wp-includes/taxonomy.php */
        do_action('edited_term_taxonomy', $term, $taxonomy);
    }
}

WordPress Version: 3.7

//
// Default callbacks
//
/**
 * Will update term count based on object types of the current taxonomy.
 *
 * Private function for the default callback for post_tag and category
 * taxonomies.
 *
 * @package WordPress
 * @subpackage Taxonomy
 * @access private
 * @since 2.3.0
 * @uses $wpdb
 *
 * @param array $terms List of Term taxonomy IDs
 * @param object $taxonomy Current taxonomy object of terms
 */
function _update_post_term_count($terms, $taxonomy)
{
    global $wpdb;
    $object_types = (array) $taxonomy->object_type;
    foreach ($object_types as &$object_type) {
        list($object_type) = explode(':', $object_type);
    }
    $object_types = array_unique($object_types);
    if (false !== $check_attachments = array_search('attachment', $object_types)) {
        unset($object_types[$check_attachments]);
        $check_attachments = true;
    }
    if ($object_types) {
        $object_types = esc_sql(array_filter($object_types, 'post_type_exists'));
    }
    foreach ((array) $terms as $term) {
        $count = 0;
        // Attachments can be 'inherit' status, we need to base count off the parent's status if so
        if ($check_attachments) {
            $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 = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM {$wpdb->posts} WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term));
        }
        if ($object_types) {
            $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 = 'publish' AND post_type IN ('" . implode("', '", $object_types) . "') AND term_taxonomy_id = %d", $term));
        }
        do_action('edit_term_taxonomy', $term, $taxonomy);
        $wpdb->update($wpdb->term_taxonomy, compact('count'), array('term_taxonomy_id' => $term));
        do_action('edited_term_taxonomy', $term, $taxonomy);
    }
}