A WordPress-centric search engine for devs and theme authors



wp_modify_term_count_by_now ›

Since5.6.0
Deprecatedn/a
wp_modify_term_count_by_now ( $tt_ids, $taxonomy, $modify_by )
Parameters: (3)
  • (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
Returns:
  • (bool) If no terms will return false, and if successful will return true.
Defined at:
Codex:

Modifies the amount of terms in taxonomy immediately

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_now( $tt_ids, $taxonomy, $modify_by ) {
	global $wpdb;

	if ( 0 === $modify_by ) {
		return false;
	}

	$tt_ids = array_filter( array_map( 'intval', (array) $tt_ids ) );

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

	$taxonomy = get_taxonomy( $taxonomy );
	if ( ! empty( $taxonomy->update_count_by_callback ) ) {
		call_user_func( $taxonomy->update_count_by_callback, $tt_ids, $taxonomy, $modify_by );
		clean_term_cache( $tt_ids, '', false );
		return true;
	}

	$tt_ids_string = '(' . implode( ',', $tt_ids ) . ')';

	foreach ( $tt_ids as $tt_id ) {
		/** This action is documented in wp-includes/taxonomy.php */
		do_action( 'edit_term_taxonomy', $tt_id, $taxonomy );
	}

	$result = $wpdb->query(
		$wpdb->prepare(
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			"UPDATE {$wpdb->term_taxonomy} AS tt SET tt.count = GREATEST( 0, tt.count + %d ) WHERE tt.term_taxonomy_id IN $tt_ids_string",
			$modify_by
		)
	);

	if ( ! $result ) {
		return false;
	}

	foreach ( $tt_ids as $tt_id ) {
		/** This action is documented in wp-includes/taxonomy.php */
		do_action( 'edited_term_taxonomy', $tt_id, $taxonomy );
	}

	clean_term_cache( $tt_ids, '', false );

	return true;
}