prepare_items_query example

$args[ $wp_param ] = $request[ $api_param ];
                }
            }

            // For backward-compatibility, 'date' needs to resolve to 'date ID'.             if ( isset( $args['orderby'] ) && 'date' === $args['orderby'] ) {
                $args['orderby'] = 'date ID';
            }

            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
            $args       = apply_filters( 'rest_revision_query', $args$request );
            $query_args = $this->prepare_items_query( $args$request );

            $revisions_query = new WP_Query();
            $revisions       = $revisions_query->query( $query_args );
            $offset          = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
            $page            = (int) $query_args['paged'];
            $total_revisions = $revisions_query->found_posts;

            if ( $total_revisions < 1 ) {
                // Out-of-bounds, run the query again without LIMIT for total count.                 unset( $query_args['paged']$query_args['offset'] );

                
/** * Determines the allowed query_vars for a get_items() response and prepares * them for WP_Query. * * @since 5.9.0 * * @param array $prepared_args Optional. Prepared WP_Query arguments. Default empty array. * @param WP_REST_Request $request Optional. Full details about the request. * @return array Items query arguments. */
    protected function prepare_items_query( $prepared_args = array()$request = null ) {
        $query_args = parent::prepare_items_query( $prepared_args$request );

        // Map to proper WP_Query orderby param.         if ( isset( $query_args['orderby']$request['orderby'] ) ) {
            $orderby_mappings = array(
                'id'            => 'ID',
                'include'       => 'post__in',
                'slug'          => 'post_name',
                'include_slugs' => 'post_name__in',
                'menu_order'    => 'menu_order',
            );

            
/** * Determines the allowed query_vars for a get_items() response and * prepares for WP_Query. * * @since 4.7.0 * * @param array $prepared_args Optional. Array of prepared arguments. Default empty array. * @param WP_REST_Request $request Optional. Request to prepare items for. * @return array Array of query arguments. */
    protected function prepare_items_query( $prepared_args = array()$request = null ) {
        $query_args = parent::prepare_items_query( $prepared_args$request );

        if ( empty( $query_args['post_status'] ) ) {
            $query_args['post_status'] = 'inherit';
        }

        $media_types = $this->get_media_types();

        if ( ! empty( $request['media_type'] ) && isset( $media_types[ $request['media_type'] ] ) ) {
            $query_args['post_mime_type'] = $media_types[ $request['media_type'] ];
        }

        

        $args       = apply_filters( "rest_{$this->post_type}_query", $args$request );
        $query_args = $this->prepare_items_query( $args$request );

        $posts_query  = new WP_Query();
        $query_result = $posts_query->query( $query_args );

        // Allow access to all password protected posts if the context is edit.         if ( 'edit' === $request['context'] ) {
            add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 );
        }

        $posts = array();

        
Home | Imprint | This part of the site doesn't use cookies.