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
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
- Scaling
- Scales with input
- Instructions
- 24–89
- Plugin surface
- None
- Called by
- 1
Reaches the database via ->get_col().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 105.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$taxonomies && !taxonomy_exists() | 24–28 | taxonomy_exists(), __() |
$cache !== false | 73–79 | wp_parse_args(), strtolower(), array_map(), array_map(), wp_cache_get_last_changed(), md5(), wp_cache_get_salted() |
$cache === false | 83–89 | wp_parse_args(), strtolower(), array_map(), array_map(), wp_cache_get_last_changed(), md5(), wp_cache_get_salted(), ->get_col(), wp_cache_set_salted() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 105 | 24–89 | 8 | |
| 8.5 | 105 | 24–89 | 8 | |
| 8.4 | 105 | 24–89 | 8 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 111 | 24–95 | 8 | |
| 8.2 | 111 | 24–95 | 8 | |
| 8.1 | 111 | 24–95 | 8 | |
| 7.4 | 111 | 24–95 | 8 |
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
- taxonomy_exists()Determines whether the taxonomy name exists.
- __()Retrieves the translation of $text.
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_cache_get_last_changed()Gets last changed date for the specified cache group.
- wp_cache_get_salted()Retrieves cached data if valid and unchanged.
- wp_cache_set_salted()Stores salted data in the cache.
- WP_Error::__construct()Initializes the error.
Used by · 1
- wp_delete_nav_menu()Deletes a navigation menu.
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 ); $cache = wp_cache_get_salted( $cache_key, 'term-queries', $last_changed ); if ( false === $cache ) { $object_ids = $wpdb->get_col( $sql ); wp_cache_set_salted( $cache_key, $object_ids, 'term-queries', $last_changed ); } 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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.