A WordPress-centric search engine for devs and theme authors



wp_modify_term_count_by ›

Since5.6.0
Deprecatedn/a
wp_modify_term_count_by ( $tt_ids, $taxonomy, $modify_by, $do_deferred = false )
Parameters: (4)
  • (int|array) $tt_ids The term_taxonomy_id of the terms.
    Required: Yes
  • (string) $taxonomy The context of the term.
    Required: Yes
  • (int) $modify_by By how many the term count is to be modified.
    Required: Yes
  • (bool) $do_deferred Whether to flush the deferred term counts too. Default false.
    Required: No
    Default: false
Returns:
  • (bool) If no terms will return false, and if successful will return true.
Defined at:
Codex:

Modifies the amount of terms in taxonomy.

If there is a taxonomy callback applied, then it will be called for updating the count. The default action is to decrement the count by one and update the database.


Source

function wp_modify_term_count_by( $tt_ids, $taxonomy, $modify_by, $do_deferred = false ) {
	static $_deferred = array();

	if ( $do_deferred ) {
		foreach ( (array) $_deferred as $taxonomy_name => $modifications ) {
			$tax_by_count = array_reduce(
				array_keys( $modifications ),
				function( $by_count, $tt_id ) use ( $modifications ) {
					if ( ! isset( $by_count[ $modifications[ $tt_id ] ] ) ) {
						$by_count[ $modifications[ $tt_id ] ] = array();
					}
					$by_count[ $modifications[ $tt_id ] ][] = $tt_id;
					return $by_count;
				},
				array()
			);

			foreach ( $tax_by_count as $_modify_by => $_tt_ids ) {
				wp_modify_term_count_by_now( $_tt_ids, $taxonomy_name, $_modify_by );
			}
			unset( $_deferred[ $taxonomy_name ] );
		}
	}

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

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

	if ( wp_defer_term_counting() ) {
		foreach ( $tt_ids as $tt_id ) {
			if ( ! isset( $_deferred[ $taxonomy ][ $tt_id ] ) ) {
				$_deferred[ $taxonomy ][ $tt_id ] = 0;
			}
			$_deferred[ $taxonomy ][ $tt_id ] += $modify_by;
		}
		return true;
	}

	return wp_modify_term_count_by_now( $tt_ids, $taxonomy, $modify_by );
}