# Category and Term Names in REST API Responses

> Adds a "term_names" field to REST API responses for posts and custom post types, so apps get category and tag names without extra requests.

- Type: PHP
- Runs: Run everywhere
- Category: [Custom Post Types & Fields](https://wpide.com/library/category/post-types/)
- Tags: rest-api, taxonomies, headless
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/term-names-in-rest/

## Code

```php
add_action( 'rest_api_init', function () {
    $types = get_post_types( array( 'show_in_rest' => true, 'public' => true ) );

    register_rest_field( array_values( $types ), 'term_names', array(
        'get_callback' => function ( $object ) {
            $names = array();

            foreach ( get_object_taxonomies( $object['type'] ?? '', 'objects' ) as $taxonomy ) {
                if ( ! $taxonomy->show_in_rest ) {
                    continue;
                }

                $terms = get_the_terms( (int) $object['id'], $taxonomy->name );
                $names[ $taxonomy->name ] = ( $terms && ! is_wp_error( $terms ) ) ? wp_list_pluck( $terms, 'name' ) : array();
            }

            return $names;
        },
        'schema'       => array( 'description' => 'Term names keyed by taxonomy.', 'type' => 'object', 'context' => array( 'view', 'edit' ), 'readonly' => true ),
    ) );
} );
```

## Notes

Only taxonomies shown in REST are included.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Category and Term Names in REST API Responses" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
