wppaste
WordPress

wp_get_object_terms( int|int[] $object_ids, string|string[] $taxonomies, array|string $args = array() ): WP_Term[]|int[]|string[]|string|WP_Error

Since
2.3.0, 4.2.0, 4.4.0, 4.7.0, 6.3.0
Source
wp-includes/taxonomy.php:2254
Retrieves the terms associated with the given object(s), in the supplied taxonomies.

Compatibility

WordPress
since 6.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$object_idsint|int[]
The ID(s) of the object(s) to retrieve.
$taxonomiesstring|string[]
The taxonomy names to retrieve terms from.
$argsarray|stringoptional
See WP_Term_Query::__construct() for supported arguments.Default: array()

Return value

WP_Term[]|int[]|string[]|string|WP_Error
Array of terms, a count thereof as a numeric string, or WP_Error if any of the taxonomies do not exist.
See WP_Term_Query::get_terms() for more information.

Performance profile

How much work a call to wp_get_object_terms() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via wp_get_object_terms().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
6–103

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 158.

Plugin surface
3 hooks

Third-party callbacks on 'wp_get_object_terms_args', 'get_object_terms', 'wp_get_object_terms' run inside this call, and their cost is not bounded by anything here.

Called by
27

27 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent querywp_get_object_terms()this function does it
  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 11 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_object_terms() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always6–8none
!empty($object_ids) && !empty($taxonomies) && !$taxonomies && !taxonomy_exists()24–26taxonomy_exists(), __()
!empty($object_ids)70–76array_map(), wp_parse_args(), apply_filters(), apply_filters(), array_map(), apply_filters()
!empty($object_ids)75–83array_map(), wp_parse_args(), apply_filters(), get_taxonomy(), apply_filters(), array_map(), apply_filters()
!empty($object_ids) && !empty($taxonomies)81–90array_map(), wp_parse_args(), apply_filters(), get_terms(), array_merge(), apply_filters(), array_map(), apply_filters()
!empty($object_ids) && !empty($taxonomies) && !empty($args)82–88array_map(), wp_parse_args(), apply_filters(), get_terms(), apply_filters(), array_map(), apply_filters()
!empty($object_ids) && isset($t)84–89array_map(), wp_parse_args(), apply_filters(), get_taxonomy(), array_merge(), apply_filters(), array_map(), apply_filters()
!empty($object_ids) && !empty($taxonomies)86–97array_map(), wp_parse_args(), apply_filters(), get_taxonomy(), get_terms(), array_merge(), apply_filters(), array_map(), apply_filters()
!empty($object_ids) && !empty($taxonomies) && !empty($args)87–95array_map(), wp_parse_args(), apply_filters(), get_taxonomy(), get_terms(), apply_filters(), array_map(), apply_filters()
!empty($object_ids) && !empty($taxonomies) && isset($t)95–103array_map(), wp_parse_args(), apply_filters(), get_taxonomy(), array_merge(), get_terms(), array_merge(), apply_filters(), array_map(), apply_filters()
!empty($object_ids) && !empty($taxonomies) && isset($t) && !empty($args)96–101array_map(), wp_parse_args(), apply_filters(), get_taxonomy(), array_merge(), get_terms(), apply_filters(), array_map(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1586–10318
8.51586–10318
8.41586–103189 fewer instructions than PHP 8.3
8.31676–11218
8.21676–11218
8.11676–11218Different branch profile from PHP 7.4
7.41676–11318

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while wp_get_object_terms() runs, in this order:

  1. apply_filters( wp_get_object_terms_args )filterline 2290 (+36 into the body)

    Filters arguments for retrieving object terms.

  2. apply_filters( get_object_terms )filterline 2338 (+84 into the body)

    Filters the terms for a given object or objects.

  3. apply_filters( wp_get_object_terms )filterline 2357 (+103 into the body)

    Filters the terms for a given object or objects.

Uses · 9

Used by · 27

Show all 27

Source code

function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {	if ( empty( $object_ids ) || empty( $taxonomies ) ) {		return array();	} 	if ( ! is_array( $taxonomies ) ) {		$taxonomies = array( $taxonomies );	} 	foreach ( $taxonomies as $taxonomy ) {		if ( ! taxonomy_exists( $taxonomy ) ) {			return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );		}	} 	if ( ! is_array( $object_ids ) ) {		$object_ids = array( $object_ids );	}	$object_ids = array_map( 'intval', $object_ids ); 	$defaults = array(		'update_term_meta_cache' => false,	); 	$args = wp_parse_args( $args, $defaults ); 	/**	 * Filters arguments for retrieving object terms.	 *	 * @since 4.9.0	 *	 * @param array    $args       An array of arguments for retrieving terms for the given object(s).	 *                             See {@see wp_get_object_terms()} for details.	 * @param int[]    $object_ids Array of object IDs.	 * @param string[] $taxonomies Array of taxonomy names to retrieve terms from.	 */	$args = apply_filters( 'wp_get_object_terms_args', $args, $object_ids, $taxonomies ); 	/*	 * When one or more queried taxonomies is registered with an 'args' array,	 * those params override the `$args` passed to this function.	 */	$terms = array();	if ( count( $taxonomies ) > 1 ) {		foreach ( $taxonomies as $index => $taxonomy ) {			$t = get_taxonomy( $taxonomy );			if ( isset( $t->args ) && is_array( $t->args ) && array_merge( $args, $t->args ) != $args ) {				unset( $taxonomies[ $index ] );				$terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) );			}		}	} else {		$t = get_taxonomy( $taxonomies[0] );		if ( isset( $t->args ) && is_array( $t->args ) ) {			$args = array_merge( $args, $t->args );		}	} 	$args['taxonomy']   = $taxonomies;	$args['object_ids'] = $object_ids; 	// Taxonomies registered without an 'args' param are handled here.	if ( ! empty( $taxonomies ) ) {		$terms_from_remaining_taxonomies = get_terms( $args ); 		// Array keys should be preserved for values of $fields that use term_id for keys.		if ( ! empty( $args['fields'] ) && str_starts_with( $args['fields'], 'id=>' ) ) {			$terms = $terms + $terms_from_remaining_taxonomies;		} else {			$terms = array_merge( $terms, $terms_from_remaining_taxonomies );		}	} 	/**	 * Filters the terms for a given object or objects.	 *	 * @since 4.2.0	 *	 * @param WP_Term[]|int[]|string[]|string $terms      Array of terms or a count thereof as a numeric string.	 * @param int[]                           $object_ids Array of object IDs for which terms were retrieved.

Changelog

Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.3.0
Passing update_term_meta_cache argument value false by default resulting in get_terms() to not prime the term meta cache.from the docblock
4.7.0
Refactored to use WP_Term_Query, and to support any WP_Term_Query arguments.from the docblock
4.4.0
Introduced $meta_query and $update_term_meta_cache arguments. When $fields is 'all' or 'all_with_object_id', an array of WP_Term objects will be returned.from the docblock
4.2.0
Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of $orderby.
Introduced $parent argument.from the docblock
2.3.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/taxonomy.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.