wppaste
WordPress

get_objects_in_term( int|int[] $term_ids, string|string[] $taxonomies, array|string $args = array() ): string[]|WP_Error

Since
2.3.0
Source
wp-includes/taxonomy.php:871
Retrieves object IDs of valid taxonomy and term.

Description

The strings of $taxonomies must exist before this function will continue.
On failure of finding a valid taxonomy, it will return a WP_Error.

The $terms aren't checked the same as $taxonomies, but still need to exist for object IDs to be returned.

It is possible to change the order that object IDs are returned by using $args with either ASC or DESC array. The value should be in the key named 'order'.

Compatibility

WordPress
since 2.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

$term_idsint|int[]
Term ID or array of term IDs of terms that will be used.
$taxonomiesstring|string[]
String of taxonomy name or Array of string values of taxonomy names.
$argsarray|stringoptional
Change the order of the object IDs.Default: array()
  • $orderstringdefault: 'ASC'

    Order to retrieve terms. Accepts 'ASC' or 'DESC'.

Return value

string[]|WP_Error
An array of object IDs as numeric strings on success, WP_Error if the taxonomy does not exist.

Performance profile

How much work a call to get_objects_in_term() 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 ->get_col().

Scaling
Scales with input

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

Instructions
24–89

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What it touches

  • cacheobject cachewp_cache_get_last_changed()called directly
  • sqldatabase query->get_col()called directly

Further down the call graph this can also reach hook, query, option, 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 · 3 distinct outcomes

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

WhenInstructionsCalls it makes
!$taxonomies && !taxonomy_exists()24–28taxonomy_exists(), __()
$cache !== false74–80wp_parse_args(), strtolower(), array_map(), array_map(), wp_cache_get_last_changed(), md5(), wp_cache_get()
$cache === false83–89wp_parse_args(), strtolower(), array_map(), array_map(), wp_cache_get_last_changed(), md5(), wp_cache_get(), ->get_col(), wp_cache_set()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10524–898
8.510524–898
8.410524–8986 fewer instructions than PHP 8.3
8.311124–958
8.211124–958
8.111124–958
7.411124–958

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.

Uses · 7

Used by · 1

Source code

function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {	global $wpdb; 	if ( ! is_array( $term_ids ) ) {		$term_ids = array( $term_ids );	}	if ( ! is_array( $taxonomies ) ) {		$taxonomies = array( $taxonomies );	}	foreach ( (array) $taxonomies as $taxonomy ) {		if ( ! taxonomy_exists( $taxonomy ) ) {			return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );		}	} 	$defaults = array( 'order' => 'ASC' );	$args     = wp_parse_args( $args, $defaults ); 	$order = ( 'desc' === strtolower( $args['order'] ) ) ? 'DESC' : 'ASC'; 	$term_ids = array_map( 'intval', $term_ids ); 	$taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'";	$term_ids   = "'" . implode( "', '", $term_ids ) . "'"; 	$sql = "SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order"; 	$last_changed = wp_cache_get_last_changed( 'terms' );	$cache_key    = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";	$cache        = wp_cache_get( $cache_key, 'term-queries' );	if ( false === $cache ) {		$object_ids = $wpdb->get_col( $sql );		wp_cache_set( $cache_key, $object_ids, 'term-queries' );	} else {		$object_ids = (array) $cache;	} 	if ( ! $object_ids ) {		return array();	}	return $object_ids;}

Changelog

Introduced in 2.3.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.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.