wppaste
WordPress

rest_is_field_included( string $field, array $fields ): bool

Since
5.3.0
Source
wp-includes/rest-api.php:1001

Checks whether a single field name is present in an array of allowed REST response fields, matching dotted parent.child names in either direction. It backs the field-filtering logic in WP_REST_Controller::add_additional_fields_to_object() and the prepare_item_for_response() methods of most core REST controllers. Because the initial comparison is a strict in_array() check, field names must match exactly in case and spelling to be recognized as a direct hit before the parent/child fallback runs.

Given an array of fields to include in a response, some of which may be nested.fields, determine whether the provided field should be included in the response body.

Description

If a parent field is passed in, the presence of any nested field within that parent will cause the method to return true. For example "title" will return true if any of title, title.raw or title.rendered is provided.

Compatibility

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

$fieldstring
A field to test for inclusion in the response body.
$fieldsarray
An array of string fields supported by the endpoint.

Return value

bool
Whether to include the field or not.

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.

Test whether a field survives a REST _fields filter, including nested dotted fields

Run the function directly against a sample requested-fields array to see how parent and child field names resolve.

$requested_fields = array( 'id', 'title.rendered', 'meta.price' );

$checks = array(
	'title'      => rest_is_field_included( 'title', $requested_fields ),
	'title.raw'  => rest_is_field_included( 'title.raw', $requested_fields ),
	'meta'       => rest_is_field_included( 'meta', $requested_fields ),
	'meta.price' => rest_is_field_included( 'meta.price', $requested_fields ),
	'author'     => rest_is_field_included( 'author', $requested_fields ),
);

echo '<pre>';
foreach ( $checks as $field => $included ) {
	printf( "%-12s => %s\n", esc_html( $field ), $included ? 'true' : 'false' );
}
echo '</pre>';

title.raw is included even though only title.rendered was requested, because the plain 'title' rule matches any of its children, but the demo asks about title.raw specifically to show that a sibling child field is not accepted on its own; check the printed output rather than assuming.

Skip an expensive REST field calculation unless it was actually requested

Register a computed field on posts that only does the work when the client's _fields list actually asks for it, either by name or as part of a parent field.

add_action( 'rest_api_init', function() {
	register_rest_field(
		'post',
		'reading_time',
		array(
			'get_callback' => function( $post_arr, $field_name, $request ) {
				$requested = $request->get_fields();

				if ( ! empty( $requested ) && ! rest_is_field_included( $field_name, $requested ) ) {
					return null;
				}

				$post  = get_post( $post_arr['id'] );
				$words = str_word_count( wp_strip_all_tags( $post->post_content ) );

				return array(
					'words'   => $words,
					'minutes' => (int) ceil( $words / 200 ),
				);
			},
			'schema' => array(
				'type'    => 'object',
				'context' => array( 'view', 'edit' ),
			),
		)
	);
} );

After activating, load /wp-json/wp/v2/posts/1?_fields=title,reading_time.minutes in a browser tab to see reading_time returned, and reload without the _fields parameter or with a different one to see it disappear.

Common problems and fixes · 3

Why isn't my requested field being included even though it's spelled right in _fields?

The very first check is a strict in_array($field, $fields, true) comparison, so anything other than an exact case and character match falls through to the parent/child logic, which may or may not rescue it. A stray space or wrong case in the _fields query parameter is a common culprit.

Why does requesting a parent field pull in fields I never asked for?

The function checks both directions: if 'parent' is in $fields, every 'parent.child' is treated as included, and if any 'parent.child' is in $fields, 'parent' itself is treated as included. This is intentional per the long description, but it surprises developers who expect field-level, not group-level, filtering.

Why does passing an empty $fields array make every field come back excluded?

With an empty $fields array, in_array() has nothing to match and the foreach loop never runs, so the function always returns false, which would hide every field. Core callers such as WP_REST_Controller::add_additional_fields_to_object() work around this by checking whether the requested fields list is empty first and including everything in that case, rather than calling this function.

Alternatives and related functions

WP_REST_Controller::get_fields_for_response
When you need the whole resolved list of fields for a request rather than testing one field name at a time.
WP_REST_Controller::add_additional_fields_to_object
When you're preparing a REST response object and want the standard additional-fields loop, which already calls this function for you.
WP_REST_Request::get_fields
When you need the parsed array of requested fields from the current request's _fields parameter to pass in as the $fields argument.

Performance profile

How much work a call to rest_is_field_included() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
6–15

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
35

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

What one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
always6–15none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev206–155
8.5206–155
8.4206–1559 fewer instructions than PHP 8.3
8.3299–245
8.2299–245
8.1299–245
7.4299–245

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

Used by · 35

Show all 35

Source code

function rest_is_field_included( $field, $fields ) {	if ( in_array( $field, $fields, true ) ) {		return true;	} 	foreach ( $fields as $accepted_field ) {		/*		 * Check to see if $field is the parent of any item in $fields.		 * A field "parent" should be accepted if "parent.child" is accepted.		 */		if ( str_starts_with( $accepted_field, "$field." ) ) {			return true;		}		/*		 * Conversely, if "parent" is accepted, all "parent.child" fields		 * should also be accepted.		 */		if ( str_starts_with( $field, "$accepted_field." ) ) {			return true;		}	} 	return false;}

Changelog

Introduced in 5.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.9.7 tag, from src/wp-includes/rest-api.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.