get_query_params example

// Store pagination values for headers.         $per_page = (int) $prepared_args['number'];
        $page     = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );

        $response->header( 'X-WP-Total', (int) $total_terms );

        $max_pages = ceil( $total_terms / $per_page );

        $response->header( 'X-WP-TotalPages', (int) $max_pages );

        $request_params = $request->get_query_params();
        $collection_url = rest_url( rest_get_route_for_taxonomy_items( $this->taxonomy ) );
        $base           = add_query_arg( urlencode_deep( $request_params )$collection_url );

        if ( $page > 1 ) {
            $prev_page = $page - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            $prev_link = add_query_arg( 'page', $prev_page$base );
            
            unset( $prepared_args['number']$prepared_args['offset'] );
            $count_query = new WP_User_Query( $prepared_args );
            $total_users = $count_query->get_total();
        }

        $response->header( 'X-WP-Total', (int) $total_users );

        $max_pages = ceil( $total_users / $per_page );

        $response->header( 'X-WP-TotalPages', (int) $max_pages );

        $base = add_query_arg( urlencode_deep( $request->get_query_params() )rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
        if ( $page > 1 ) {
            $prev_page = $page - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            $prev_link = add_query_arg( 'page', $prev_page$base );
            $response->link_header( 'prev', $prev_link );
        }
        if ( $max_pages > $page ) {
            
foreach ( $revisions as $revision ) {
            $data       = $this->prepare_item_for_response( $revision$request );
            $response[] = $this->prepare_response_for_collection( $data );
        }

        $response = rest_ensure_response( $response );

        $response->header( 'X-WP-Total', (int) $total_revisions );
        $response->header( 'X-WP-TotalPages', (int) $max_pages );

        $request_params = $request->get_query_params();
        $base_path      = rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent']$this->rest_base ) );
        $base           = add_query_arg( urlencode_deep( $request_params )$base_path );

        if ( $page > 1 ) {
            $prev_page = $page - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            $prev_link = add_query_arg( 'page', $prev_page$base );
            
return new WP_Error(
                'rest_search_invalid_page_number',
                __( 'The page number requested is larger than the number of pages available.' ),
                array( 'status' => 400 )
            );
        }

        $response = rest_ensure_response( $results );
        $response->header( 'X-WP-Total', $total );
        $response->header( 'X-WP-TotalPages', $max_pages );

        $request_params = $request->get_query_params();
        $base           = add_query_arg( urlencode_deep( $request_params )rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );

        if ( $page > 1 ) {
            $prev_link = add_query_arg( 'page', $page - 1, $base );
            $response->link_header( 'prev', $prev_link );
        }
        if ( $page < $max_pages ) {
            $next_link = add_query_arg( 'page', $page + 1, $base );
            $response->link_header( 'next', $next_link );
        }

        
'rest_post_invalid_page_number',
                __( 'The page number requested is larger than the number of pages available.' ),
                array( 'status' => 400 )
            );
        }

        $response = rest_ensure_response( $posts );

        $response->header( 'X-WP-Total', (int) $total_posts );
        $response->header( 'X-WP-TotalPages', (int) $max_pages );

        $request_params = $request->get_query_params();
        $collection_url = rest_url( rest_get_route_for_post_type_items( $this->post_type ) );
        $base           = add_query_arg( urlencode_deep( $request_params )$collection_url );

        if ( $page > 1 ) {
            $prev_page = $page - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            $prev_link = add_query_arg( 'page', $prev_page$base );
            
$query                  = new WP_Comment_Query();
            $prepared_args['count'] = true;

            $total_comments = $query->query( $prepared_args );
            $max_pages      = ceil( $total_comments / $request['per_page'] );
        }

        $response = rest_ensure_response( $comments );
        $response->header( 'X-WP-Total', $total_comments );
        $response->header( 'X-WP-TotalPages', $max_pages );

        $base = add_query_arg( urlencode_deep( $request->get_query_params() )rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );

        if ( $request['page'] > 1 ) {
            $prev_page = $request['page'] - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            $prev_link = add_query_arg( 'page', $prev_page$base );
            $response->link_header( 'prev', $prev_link );
        }

        
foreach ( $revisions as $revision ) {
            $data       = $this->prepare_item_for_response( $revision$request );
            $response[] = $this->prepare_response_for_collection( $data );
        }

        $response = rest_ensure_response( $response );

        $response->header( 'X-WP-Total', (int) $total_revisions );
        $response->header( 'X-WP-TotalPages', (int) $max_pages );

        $request_params = $request->get_query_params();
        $base_path      = rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent']$this->rest_base ) );
        $base           = add_query_arg( urlencode_deep( $request_params )$base_path );

        if ( $page > 1 ) {
            $prev_page = $page - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            $prev_link = add_query_arg( 'page', $prev_page$base );
            
Home | Imprint | This part of the site doesn't use cookies.