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:
- 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
- __()Retrieves the translation of $text.
- get_option()Retrieves an option value based on an option name.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- add_filter()Adds a callback function to a filter hook.
- update_post_author_caches()Updates post author user caches for a list of post objects.
- update_post_parent_caches()Updates parent post caches for a list of post objects.
- post_type_supports()Checks a post type's support for a given feature.
- update_post_thumbnail_cache()Updates cache for thumbnails in the current loop.
- remove_filter()Removes a callback function from a filter hook.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- rest_url()Retrieves the URL to a REST endpoint.
- rest_get_route_for_post_type_items()Gets the REST API route for a post type.
Show all 24
- add_query_arg()Retrieves a modified URL query string.
- urlencode_deep()Navigates through an array, object, or scalar, and encodes the values to be used in a URL.
- WP_Error::__construct()Initializes the error.
- WP_REST_Posts_Controller::get_collection_params()Retrieves the query params for the posts collection.
- WP_REST_Posts_Controller::prepare_tax_query()Prepares the 'tax_query' for a collection of posts.
- WP_REST_Posts_Controller::prepare_items_query()Determines the allowed query_vars for a get_items() response and prepares them for WP_Query.
- WP_Query::__construct()Constructor.
- WP_REST_Posts_Controller::check_update_permission()Checks if a post can be edited.
- WP_REST_Posts_Controller::check_read_permission()Checks if a post can be read.
- WP_REST_Posts_Controller::prepare_item_for_response()Prepares a single post output for response.
- WP_REST_Posts_Controller::prepare_response_for_collection()
- WP_REST_Response::__construct()
Used by · 1
- WP_REST_Font_Faces_Controller::get_items()Retrieves a collection of font faces within the parent font family.
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.
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.