wp_count_terms() WordPress Function

The wp_count_terms() function is used to count the number of terms in a given taxonomy. It accepts two parameters: the taxonomy name and a term name. The function returns an integer representing the number of terms in the taxonomy.

wp_count_terms( array|string $args = array(), array|string $deprecated = '' ) #

Counts how many terms are in taxonomy.


Description

Default $args is ‘hide_empty’ which can be ‘hide_empty=true’ or array(‘hide_empty’ => true).


Top ↑

Parameters

$args

(array|string)(Optional) Array of arguments that get passed to get_terms().

Default value: array()

$deprecated

(array|string)(Optional) Argument array, when using the legacy function parameter format. If present, this parameter will be interpreted as $args, and the first function parameter will be parsed as a taxonomy or array of taxonomies.

Default value: ''


Top ↑

Return

(string|WP_Error) Numeric string containing the number of terms in that taxonomy or WP_Error if the taxonomy does not exist.


Top ↑

Source

File: wp-includes/taxonomy.php

function wp_count_terms( $args = array(), $deprecated = '' ) {
	$use_legacy_args = false;

	// Check whether function is used with legacy signature: `$taxonomy` and `$args`.
	if ( $args
		&& ( is_string( $args ) && taxonomy_exists( $args )
			|| is_array( $args ) && wp_is_numeric_array( $args ) )
	) {
		$use_legacy_args = true;
	}

	$defaults = array( 'hide_empty' => false );

	if ( $use_legacy_args ) {
		$defaults['taxonomy'] = $args;
		$args                 = $deprecated;
	}

	$args = wp_parse_args( $args, $defaults );

	// Backward compatibility.
	if ( isset( $args['ignore_empty'] ) ) {
		$args['hide_empty'] = $args['ignore_empty'];
		unset( $args['ignore_empty'] );
	}

	$args['fields'] = 'count';

	return get_terms( $args );
}


Top ↑

Changelog

Changelog
VersionDescription
5.6.0Changed the function signature so that the $args array can be provided as the first parameter.
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
Show More