WP_REST_Posts_Controller::prepare_items_query PHP Method

prepare_items_query() protected method

Determines the allowed query_vars for a get_items() response and prepares them for WP_Query.
Since: 4.7.0
protected prepare_items_query ( array $prepared_args = [], WP_REST_Request $request = null ) : array
$prepared_args array Optional. Prepared WP_Query arguments. Default empty array.
$request WP_REST_Request Optional. Full details about the request.
return array Items query arguments.
    protected function prepare_items_query($prepared_args = array(), $request = null)
    {
        $query_args = array();
        foreach ($prepared_args as $key => $value) {
            /**
             * Filters the query_vars used in get_items() for the constructed query.
             *
             * The dynamic portion of the hook name, `$key`, refers to the query_var key.
             *
             * @since 4.7.0
             *
             * @param string $value The query_var value.
             */
            $query_args[$key] = apply_filters("rest_query_var-{$key}", $value);
        }
        if ('post' !== $this->post_type || !isset($query_args['ignore_sticky_posts'])) {
            $query_args['ignore_sticky_posts'] = true;
        }
        if ('include' === $query_args['orderby']) {
            $query_args['orderby'] = 'post__in';
        }
        return $query_args;
    }

Usage Example

コード例 #1
0
 /**
  * Determine the allowed query_vars for a get_items() response and
  * prepare for WP_Query.
  *
  * @param array $prepared_args
  * @return array $query_args
  */
 protected function prepare_items_query($prepared_args = array())
 {
     $query_args = parent::prepare_items_query($prepared_args);
     if (empty($query_args['post_status']) || !in_array($query_args['post_status'], array('inherit', 'private', 'trash'))) {
         $query_args['post_status'] = 'inherit';
     }
     return $query_args;
 }
All Usage Examples Of WP_REST_Posts_Controller::prepare_items_query