get_object_taxonomies( string|string[]|WP_Post $object_type, string $output = 'names' ): string[]|WP_Taxonomy[]
- Since
- 2.3.0
- Source
wp-includes/taxonomy.php:311
Lists the taxonomies attached to a post type, an array of post types, or a post object, returned as taxonomy names or as full WP_Taxonomy objects. It reads from the global $wp_taxonomies registry, so it only sees taxonomies that have already been registered by the time it runs. Passing an attachment object routes the call to get_attachment_taxonomies() instead of doing the normal object_type match.
Description
Example:
$taxonomies = get_object_taxonomies( 'post' ); This results in:
Array( 'category', 'post_tag' )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
$object_typestring|string[]|WP_Post- Name of the type of taxonomy object, or an object (row from posts).
$outputstringoptional- The type of output to return in the array. Accepts either 'names' or 'objects'. Default 'names'.Default:
'names'
Return value
string[]|WP_Taxonomy[]- The names or objects of all taxonomies of
$object_type.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Get the taxonomies registered for the post post type
Print the taxonomy names that WordPress ties to standard posts.
$taxonomies = get_object_taxonomies( 'post' );
echo '<pre>' . esc_html( print_r( $taxonomies, true ) ) . '</pre>';Core registers category and post_tag on the post post type, so both names show up by default.
Get taxonomy objects for a custom post type instead of just names
Register a book post type with its own genre taxonomy, then pull back full WP_Taxonomy objects for it.
$taxonomies = get_object_taxonomies( 'book', 'objects' );
foreach ( $taxonomies as $taxonomy ) {
echo esc_html( $taxonomy->label ) . '<br>';
}Anything other than the literal string 'names' for $output returns objects, so a typo like 'object' still works.
Common problems and fixes · 4
- Why does get_object_taxonomies() return an empty array for my custom post type?
- Why did passing $post_type as a WP_Post object give me totally different results?
- Why does $output = 'object' (singular) still give me objects instead of an error?
- Can I pass an array of post types to get taxonomies shared across several types?
Why does get_object_taxonomies() return an empty array for my custom post type?
Why did passing $post_type as a WP_Post object give me totally different results?
Why does $output = 'object' (singular) still give me objects instead of an error?
Can I pass an array of post types to get taxonomies shared across several types?
Alternatives and related functions
get_taxonomies- When you need every registered taxonomy in the site regardless of which post types or objects it is attached to.
get_attachment_taxonomies- When you are specifically working with attachments, since get_object_taxonomies() delegates to this function internally for that case.
is_object_in_taxonomy- When you only need a yes or no answer for whether one specific object type is registered in one specific taxonomy.
wp_get_object_terms- When you need the actual term assignments on an object, not just the names of the taxonomies it belongs to.
Performance profile
How much work a call to get_object_taxonomies() 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
- 12–18
- Plugin surface
- None
- Called by
- 38
Reaches the database via get_post().
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 39.
Nothing here hands control to plugin code.
38 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()one call below get_object_taxonomies()
Further down the call graph this can also reach hook, cache, serialize, option 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 · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_object_taxonomies() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 12–18 | none |
is_object($object_type) | 13 | get_attachment_taxonomies() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 39 instructions, 12–18 executed per call, 6 branches. The work does not change between versions.
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 · 1
- get_attachment_taxonomies()Retrieves taxonomies attached to given the attachment.
Used by · 38
- WP_Post_Type::unregister_taxonomies()Removes the post type from all taxonomies.
- WP_Posts_List_Table::get_columns()
- WP_Posts_List_Table::inline_edit()Outputs the hidden row displayed when inline editing
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_REST_Menu_Items_Controller::get_item_schema()Retrieves the nav menu item's schema, conforming to JSON Schema.
- WP_REST_Menu_Items_Controller::prepare_item_for_response()Prepares a single nav menu item output for response.
- WP_REST_Post_Types_Controller::prepare_item_for_response()Prepares a post type object for serialization.
- WP_REST_Posts_Controller::check_assign_terms_permission()Checks whether current user can assign all terms sent with the current request.
- WP_REST_Posts_Controller::get_available_actions()Gets the link relations available for the post and current user.
- WP_REST_Posts_Controller::get_item_schema()Retrieves the post's schema, conforming to JSON Schema.
- WP_REST_Posts_Controller::get_schema_links()Retrieves Link Description Objects that should be added to the Schema for the posts collection.
- WP_REST_Posts_Controller::handle_terms()Updates the post's terms from a REST request.
Show all 38
- WP_REST_Posts_Controller::prepare_item_for_response()Prepares a single post output for response.
- WP_REST_Posts_Controller::prepare_links()Prepares links for the request.
- WP_REST_Posts_Controller::prepare_tax_query()Prepares the 'tax_query' for a collection of posts.
- WP_REST_Posts_Controller::prepare_taxonomy_limit_schema()Prepares the collection schema for including and excluding items by terms.
- WP_REST_Taxonomies_Controller::get_items()Retrieves all public taxonomies.
- WP_REST_Taxonomies_Controller::get_items_permissions_check()Checks whether a given request has permission to read taxonomies.
- _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.
- clean_object_term_cache()Removes the taxonomy relationship to terms from the cache.
- get_attachment_taxonomies()Retrieves taxonomies attached to given the attachment.
- get_inline_data()Adds hidden fields with the data for use in the inline editor for posts and pages.
- get_post_taxonomies()Retrieves all taxonomy names for the given post.
- get_the_taxonomies()Retrieves all taxonomies associated with a post.
- is_object_in_taxonomy()Determines if the given object type is associated with the given taxonomy.
- register_and_do_post_meta_boxes()Registers the default post meta boxes, and runs the `do_meta_boxes` actions.
- update_object_term_cache()Updates the cache for the given term object ID(s).
- wp_delete_attachment()Trashes or deletes an attachment.
- wp_delete_post()Trashes or deletes a post or page.
- wp_insert_post()Inserts or update a post.
- wp_publish_post()Publishes a post by transitioning the post status.
- wp_queue_posts_for_term_meta_lazyload()Queues posts for lazy-loading of term meta.
- wp_xmlrpc_server::_insert_post()Helper method for wp_newPost() and wp_editPost(), containing shared logic.
- wp_xmlrpc_server::_prepare_post()Prepares post data for return in an XML-RPC object.
- wp_xmlrpc_server::_prepare_post_type()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 get_object_taxonomies( $object_type, $output = 'names' ) { global $wp_taxonomies; if ( is_object( $object_type ) ) { if ( 'attachment' === $object_type->post_type ) { return get_attachment_taxonomies( $object_type, $output ); } $object_type = $object_type->post_type; } $object_type = (array) $object_type; $taxonomies = array(); foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) { if ( array_intersect( $object_type, (array) $tax_obj->object_type ) ) { if ( 'names' === $output ) { $taxonomies[] = $tax_name; } else { $taxonomies[ $tax_name ] = $tax_obj; } } } return $taxonomies;}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 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.