WP_REST_Posts_Controller::get_collection_params PHP Method

get_collection_params() public method

Retrieves the query params for the posts collection.
Since: 4.7.0
public get_collection_params ( ) : array
return array Collection parameters.
    public function get_collection_params()
    {
        $params = parent::get_collection_params();
        $params['context']['default'] = 'view';
        $params['after'] = array('description' => __('Limit response to posts published after a given ISO8601 compliant date.'), 'type' => 'string', 'format' => 'date-time');
        if (post_type_supports($this->post_type, 'author')) {
            $params['author'] = array('description' => __('Limit result set to posts assigned to specific authors.'), 'type' => 'array', 'items' => array('type' => 'integer'), 'default' => array());
            $params['author_exclude'] = array('description' => __('Ensure result set excludes posts assigned to specific authors.'), 'type' => 'array', 'items' => array('type' => 'integer'), 'default' => array());
        }
        $params['before'] = array('description' => __('Limit response to posts published before a given ISO8601 compliant date.'), 'type' => 'string', 'format' => 'date-time');
        $params['exclude'] = array('description' => __('Ensure result set excludes specific IDs.'), 'type' => 'array', 'items' => array('type' => 'integer'), 'default' => array());
        $params['include'] = array('description' => __('Limit result set to specific IDs.'), 'type' => 'array', 'items' => array('type' => 'integer'), 'default' => array());
        if ('page' === $this->post_type || post_type_supports($this->post_type, 'page-attributes')) {
            $params['menu_order'] = array('description' => __('Limit result set to posts with a specific menu_order value.'), 'type' => 'integer');
        }
        $params['offset'] = array('description' => __('Offset the result set by a specific number of items.'), 'type' => 'integer');
        $params['order'] = array('description' => __('Order sort attribute ascending or descending.'), 'type' => 'string', 'default' => 'desc', 'enum' => array('asc', 'desc'));
        $params['orderby'] = array('description' => __('Sort collection by object attribute.'), 'type' => 'string', 'default' => 'date', 'enum' => array('date', 'relevance', 'id', 'include', 'title', 'slug'));
        if ('page' === $this->post_type || post_type_supports($this->post_type, 'page-attributes')) {
            $params['orderby']['enum'][] = 'menu_order';
        }
        $post_type_obj = get_post_type_object($this->post_type);
        if ($post_type_obj->hierarchical || 'attachment' === $this->post_type) {
            $params['parent'] = array('description' => __('Limit result set to those of particular parent IDs.'), 'type' => 'array', 'items' => array('type' => 'integer'), 'default' => array());
            $params['parent_exclude'] = array('description' => __('Limit result set to all items except those of a particular parent ID.'), 'type' => 'array', 'items' => array('type' => 'integer'), 'default' => array());
        }
        $params['slug'] = array('description' => __('Limit result set to posts with one or more specific slugs.'), 'type' => 'array', 'items' => array('type' => 'string'), 'sanitize_callback' => 'wp_parse_slug_list');
        $params['status'] = array('default' => 'publish', 'description' => __('Limit result set to posts assigned one or more statuses.'), 'type' => 'array', 'items' => array('enum' => array_merge(array_keys(get_post_stati()), array('any')), 'type' => 'string'), 'sanitize_callback' => array($this, 'sanitize_post_statuses'));
        $taxonomies = wp_list_filter(get_object_taxonomies($this->post_type, 'objects'), array('show_in_rest' => true));
        foreach ($taxonomies as $taxonomy) {
            $base = !empty($taxonomy->rest_base) ? $taxonomy->rest_base : $taxonomy->name;
            $params[$base] = array('description' => sprintf(__('Limit result set to all items that have the specified term assigned in the %s taxonomy.'), $base), 'type' => 'array', 'items' => array('type' => 'integer'), 'default' => array());
            $params[$base . '_exclude'] = array('description' => sprintf(__('Limit result set to all items except those that have the specified term assigned in the %s taxonomy.'), $base), 'type' => 'array', 'items' => array('type' => 'integer'), 'default' => array());
        }
        if ('post' === $this->post_type) {
            $params['sticky'] = array('description' => __('Limit result set to items that are sticky.'), 'type' => 'boolean');
        }
        /**
         * Filter collection parameters for the posts controller.
         *
         * The dynamic part of the filter `$this->post_type` refers to the post
         * type slug for the controller.
         *
         * This filter registers the collection parameter, but does not map the
         * collection parameter to an internal WP_Query parameter. Use the
         * `rest_{$this->post_type}_query` filter to set WP_Query parameters.
         *
         * @since 4.7.0
         *
         * @param $params JSON Schema-formatted collection parameters.
         * @param WP_Post_Type $post_type_obj Post type object.
         */
        return apply_filters("rest_{$this->post_type}_collection_params", $params, $post_type_obj);
    }

Usage Example

 /**
  * Get the query params for collections of attachments.
  *
  * @return array
  */
 public function get_collection_params()
 {
     $params = parent::get_collection_params();
     $params['author']['sanitize_callback'] = array($this, 'parse_author_list');
     $params['author_exclude']['sanitize_callback'] = array($this, 'parse_author_list');
     return $params;
 }
All Usage Examples Of WP_REST_Posts_Controller::get_collection_params