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
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
- Scaling
- Scales with input
- Instructions
- 6–103
- Plugin surface
- 3 hooks
- Called by
- 27
Reaches the database via wp_get_object_terms().
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 158.
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.
27 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
wp_get_object_terms()this function does it - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–8 | none |
!empty($object_ids) && !empty($taxonomies) && !$taxonomies && !taxonomy_exists() | 24–26 | taxonomy_exists(), __() |
!empty($object_ids) | 70–76 | array_map(), wp_parse_args(), apply_filters(), apply_filters(), array_map(), apply_filters() |
!empty($object_ids) | 75–83 | array_map(), wp_parse_args(), apply_filters(), get_taxonomy(), apply_filters(), array_map(), apply_filters() |
!empty($object_ids) && !empty($taxonomies) | 81–90 | array_map(), wp_parse_args(), apply_filters(), get_terms(), array_merge(), apply_filters(), array_map(), apply_filters() |
!empty($object_ids) && !empty($taxonomies) && !empty($args) | 82–88 | array_map(), wp_parse_args(), apply_filters(), get_terms(), apply_filters(), array_map(), apply_filters() |
!empty($object_ids) && isset($t) | 84–89 | array_map(), wp_parse_args(), apply_filters(), get_taxonomy(), array_merge(), apply_filters(), array_map(), apply_filters() |
!empty($object_ids) && !empty($taxonomies) | 86–97 | array_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–95 | array_map(), wp_parse_args(), apply_filters(), get_taxonomy(), get_terms(), apply_filters(), array_map(), apply_filters() |
!empty($object_ids) && !empty($taxonomies) && isset($t) | 95–103 | array_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–101 | array_map(), wp_parse_args(), apply_filters(), get_taxonomy(), array_merge(), get_terms(), apply_filters(), array_map(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 158 | 6–103 | 18 | |
| 8.5 | 158 | 6–103 | 18 | |
| 8.4 | 158 | 6–103 | 18 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 167 | 6–112 | 18 | |
| 8.2 | 167 | 6–112 | 18 | |
| 8.1 | 167 | 6–112 | 18 | Different branch profile from PHP 7.4 |
| 7.4 | 167 | 6–113 | 18 |
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:
- apply_filters( wp_get_object_terms_args )filterline 2290 (+36 into the body)
Filters arguments for retrieving object terms.
- apply_filters( get_object_terms )filterline 2338 (+84 into the body)
Filters the terms for a given object or objects.
- apply_filters( wp_get_object_terms )filterline 2357 (+103 into the body)
Filters the terms for a given object or objects.
Uses · 9
- taxonomy_exists()Determines whether the taxonomy name exists.
- __()Retrieves the translation of $text.
- wp_parse_args()Merges user defined arguments into defaults array.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_taxonomy()Retrieves the taxonomy object of $taxonomy.
- wp_get_object_terms()Retrieves the terms associated with the given object(s), in the supplied taxonomies.
- get_terms()Retrieves the terms in a given taxonomy or list of taxonomies.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- WP_Error::__construct()Initializes the error.
Used by · 27
- WP_REST_Terms_Controller::get_items()Retrieves terms associated with a taxonomy.
- _update_term_count_on_transition_post_status()Updates the custom taxonomies' term counts when a post's status is changed.
- _wp_menu_item_classes_by_context()Adds the class property classes for the current context, if applicable.
- bulk_edit_posts()Processes the post data for the bulk editing of posts.
- get_adjacent_post()Retrieves the adjacent post.
- get_attachment_fields_to_edit()Retrieves the attachment fields to edit form fields.
- get_bookmark()Retrieves bookmark data.
- get_boundary_post()Retrieves the boundary post.
- get_compat_media_markup()
- get_inline_data()Adds hidden fields with the data for use in the inline editor for posts and pages.
- get_terms_to_edit()Gets comma-separated list of terms available to edit for the given post ID.
- get_the_taxonomies()Retrieves all taxonomies associated with a post.
Show all 27
- get_the_terms()Retrieves the terms of the taxonomy that are attached to the post.
- is_object_in_term()Determines if the given object is associated with any of the given terms.
- update_object_term_cache()Updates the cache for the given term object ID(s).
- wp_delete_object_term_relationships()Unlinks the object from the taxonomy or taxonomies.
- wp_delete_term()Removes a term from the database.
- wp_get_link_cats()Retrieves the link category IDs associated with the link specified.
- wp_get_object_terms()Retrieves the terms associated with the given object(s), in the supplied taxonomies.
- wp_get_post_categories()Retrieves the list of categories for a post.
- wp_get_post_terms()Retrieves the terms for a post.
- wp_insert_post()Inserts or update a post.
- wp_popular_terms_checklist()Retrieves a list of the most popular terms from the specified taxonomy.
- wp_set_object_terms()Creates term and taxonomy relationships.
- wp_terms_checklist()Outputs an unordered list of checkbox input elements labelled with term names.
- wp_xmlrpc_server::_prepare_post()Prepares post data for return in an XML-RPC object.
- wxr_post_taxonomy()Outputs list of taxonomy terms, in XML tag format, associated with a post.
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.
Signature, return type and hooks compared across 5 parsed releases.
update_term_meta_cache argument value false by default resulting in get_terms() to not prime the term meta cache.from the docblock$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$orderby.Introduced
$parent argument.from the docblockAbout 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.