A WordPress-centric search engine for devs and theme authors



_update_term_count_on_transition_post_status ›

Since3.3.0
Deprecatedn/a
_update_term_count_on_transition_post_status ( $new_status, $old_status, $post )
Access:
  • private
Parameters: (3)
  • (string) $new_status New post status.
    Required: Yes
  • (string) $old_status Old post status.
    Required: Yes
  • (WP_Post) $post Post object.
    Required: Yes
Defined at:
Codex:

Update the custom taxonomies' term counts when a post's status is changed.

For example, default posts term counts (for custom taxonomies) don't include private / draft posts.


Source

function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) {
	if ( 'inherit' === $new_status ) {
		$new_status = get_post_status( $post->post_parent );
	}

	if ( 'inherit' === $old_status ) {
		$old_status = get_post_status( $post->post_parent );
	}

	$count_new = 'publish' === $new_status;
	$count_old = 'publish' === $old_status;

	if ( $count_new === $count_old ) {
		// Nothing to do.
		return;
	}

	/*
	 * Update counts for the post's terms.
	 *
	 * Term counting is deferred while incrementing/decrementing the counts to
	 * reduce the number of database queries required. Once the counts are
	 * complete the updates are performed if term counting wasn't previously
	 * deferred.
	 */
	$previous_deferred_setting = wp_defer_term_counting();
	wp_defer_term_counting( true );
	foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) {
		$tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) );

		if ( empty( $tt_ids ) ) {
			// No terms for this taxonomy on object.
			continue;
		}

		$object_types = (array) get_taxonomy( $taxonomy )->object_type;

		foreach ( $object_types as &$object_type ) {
			list( $object_type ) = explode( ':', $object_type );
		}

		$object_types = array_unique( $object_types );

		if ( ! in_array( $post->post_type, $object_types, true ) ) {
			$modify_by = 0;
		} elseif ( $count_new && ! $count_old ) {
			$modify_by = 1;
		} elseif ( $count_old && ! $count_new ) {
			$modify_by = -1;
		}

		if ( 'attachment' === $post->post_type ) {
			wp_modify_term_count_by( $tt_ids, $taxonomy, $modify_by );
			continue;
		}

		$check_attachments = array_search( 'attachment', $object_types, true );
		if ( false !== $check_attachments ) {
			unset( $object_types[ $check_attachments ] );
			$check_attachments = true;
		}

		wp_modify_term_count_by( $tt_ids, $taxonomy, $modify_by );
		if ( ! $check_attachments ) {
			continue;
		}

		/*
		 * For non-attachments, check if there are any attachment children
		 * with 'inherited' post status -- if so those will need to be counted.
		 */
		$attachments = get_children(
			array(
				'post_parent'            => $post->ID,
				'post_status'            => 'inherit',
				'post_type'              => 'attachment',
				'update_post_meta_cache' => false,
				'update_post_term_cache' => true,
			)
		);

		foreach ( $attachments as $attachment ) {
			_update_term_count_on_transition_post_status( $new_status, $old_status, $attachment );
		}
	}
	wp_defer_term_counting( $previous_deferred_setting );
}