wp_update_term_count() WordPress Function

This function updates the count of a term in the database. It is called whenever a post is created or edited.

wp_update_term_count( int|array $terms, string $taxonomy, bool $do_deferred = false ) #

Updates the amount of terms in taxonomy.


Description

If there is a taxonomy callback applied, then it will be called for updating the count.

The default action is to count what the amount of terms have the relationship of term ID. Once that is done, then update the database.


Top ↑

Parameters

$terms

(int|array)(Required)The term_taxonomy_id of the terms.

$taxonomy

(string)(Required)The context of the term.

$do_deferred

(bool)(Optional)Whether to flush the deferred term counts too.

Default value: false


Top ↑

Return

(bool) If no terms will return false, and if successful will return true.


Top ↑

Source

File: wp-includes/taxonomy.php

function wp_update_term_count( $terms, $taxonomy, $do_deferred = false ) {
	static $_deferred = array();

	if ( $do_deferred ) {
		foreach ( (array) array_keys( $_deferred ) as $tax ) {
			wp_update_term_count_now( $_deferred[ $tax ], $tax );
			unset( $_deferred[ $tax ] );
		}
	}

	if ( empty( $terms ) ) {
		return false;
	}

	if ( ! is_array( $terms ) ) {
		$terms = array( $terms );
	}

	if ( wp_defer_term_counting() ) {
		if ( ! isset( $_deferred[ $taxonomy ] ) ) {
			$_deferred[ $taxonomy ] = array();
		}
		$_deferred[ $taxonomy ] = array_unique( array_merge( $_deferred[ $taxonomy ], $terms ) );
		return true;
	}

	return wp_update_term_count_now( $terms, $taxonomy );
}


Top ↑

Changelog

Changelog
VersionDescription
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More