wppaste
WordPress

WP_REST_Posts_Controller::get_items( WP_REST_Request $request ): WP_REST_Response|WP_Error

Since
4.7.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:215
Retrieves a collection of posts.

Parameters

$requestWP_REST_Request
Full details about the request.

Return

WP_REST_Response|WP_Error
Response object on success, or WP_Error object on failure.

Hooks fired · 1

One hook fires while WP_REST_Posts_Controller::get_items() runs, in this order:

  1. apply_filters( rest_{$this->post_type}_query )filterline 444 (+229 into the body)

    Filters WP_Query arguments when querying posts via the REST API.

Uses · 24

Show all 24

Used by · 1

Source

	public function get_items( $request ) { 		// Ensure a search string is set in case the orderby is set to 'relevance'.		if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {			return new WP_Error(				'rest_no_search_term_defined',				__( 'You need to define a search term to order by relevance.' ),				array( 'status' => 400 )			);		} 		// Ensure an include parameter is set in case the orderby is set to 'include'.		if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {			return new WP_Error(				'rest_orderby_include_missing_include',				__( 'You need to define an include parameter to order by include.' ),				array( 'status' => 400 )			);		} 		// Retrieve the list of registered collection query parameters.		$registered = $this->get_collection_params();		$args       = array(); 		/*		 * This array defines mappings between public API query parameters whose		 * values are accepted as-passed, and their internal WP_Query parameter		 * name equivalents (some are the same). Only values which are also		 * present in $registered will be set.		 */		$parameter_mappings = array(			'author'         => 'author__in',			'author_exclude' => 'author__not_in',			'exclude'        => 'post__not_in',			'include'        => 'post__in',			'ignore_sticky'  => 'ignore_sticky_posts',			'menu_order'     => 'menu_order',			'offset'         => 'offset',			'order'          => 'order',			'orderby'        => 'orderby',			'page'           => 'paged',			'parent'         => 'post_parent__in',			'parent_exclude' => 'post_parent__not_in',			'search'         => 's',			'search_columns' => 'search_columns',			'slug'           => 'post_name__in',			'status'         => 'post_status',		); 		/*		 * For each known parameter which is both registered and present in the request,		 * set the parameter's value on the query $args.		 */		foreach ( $parameter_mappings as $api_param => $wp_param ) {			if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {				$args[ $wp_param ] = $request[ $api_param ];			}		} 		// Check for & assign any parameters which require special handling or setting.		$args['date_query'] = array(); 		if ( isset( $registered['before'], $request['before'] ) ) {			$args['date_query'][] = array(				'before' => $request['before'],				'column' => 'post_date',			);		} 		if ( isset( $registered['modified_before'], $request['modified_before'] ) ) {			$args['date_query'][] = array(				'before' => $request['modified_before'],				'column' => 'post_modified',			);		} 		if ( isset( $registered['after'], $request['after'] ) ) {			$args['date_query'][] = array(				'after'  => $request['after'],				'column' => 'post_date',

History

Introduced in 4.7.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 7.1.0 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.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.