Custom endpoint and X-WP-TotalPages (headers)

I have a custom endpoint defined like so:

    add_action( 'rest_api_init', function () {
        register_rest_route( 'menc/v1', '/crosscat/(?P<dept>[w-]+)/(?P<cat>[w-]+)', array(
            'methods' => 'GET',
            'callback' => 'dept_cat_api',
            'args' => array(
                'dept' => array(
                    'sanitize_callback' => function($param, $request, $key) {
                        return sanitize_text_field( $param );
                    }
                ),
                'cat' => array(
                    'sanitize_callback' => function($param, $request, $key) {
                        return sanitize_text_field( $param );
                    }
                ),
                'per_page' => array(
                    'default' => 10,
                    'sanitize_callback' => 'absint',
                ),
                'page' => array(
                    'default' => 1,
                    'sanitize_callback' => 'absint',
                ),
                'orderby' => array(
                    'default' => 'date',
                    'sanitize_callback' => 'sanitize_text_field',
                ),
                'order' => array(
                    'default' => 'desc',
                    'sanitize_callback' => 'sanitize_text_field',
                ),
            ),
        ) );
    } );

    function dept_cat_api( $request ) {
        $args = array(
            'post_type' => 'post',
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'crosspost',
                    'field'    => 'slug',
                    'terms'    => array( $request['dept'] ),
                ),
                array(
                    'taxonomy' => 'category',
                    'field'    => 'slug',
                    'terms'    => array( $request['cat'] ),
                ),
            ),
            'per_page' => $request['per_page'],
            'page' => $request['page'],
            'orderby' => $request['orderby'],
            'order' => $request['order']
        );

        $posts = get_posts( $args );

        if ( empty( $posts ) ) return new WP_Error( 'no_posts', 'Invalid term(s)', array( 'status' => 404 ) );

        $controller = new WP_REST_Posts_Controller('post');

        foreach ( $posts as $post ) {
            $response = $controller->prepare_item_for_response( $post, $request );
            $data[] = $controller->prepare_response_for_collection( $response );  
        }

// return results
        return new WP_REST_Response($data, 200);

    }

Remotely I’m having a problem. When I call the route everything seems fine with the content, however both X-WP-TotalPages and X-WP-Total return empty responses.

    object(Requests_Utility_CaseInsensitiveDictionary)#991 (1) {
      ["data":protected]=>
      array(20) {
        ["server"]=>
        string(5) "nginx"
        ["date"]=>
        string(29) "Wed, 14 Jun 2017 14:07:51 GMT"
        ["content-type"]=>
        string(31) "application/json; charset=UTF-8"
        ["content-length"]=>
        string(6) "104787"
        ["expires"]=>
        string(29) "Thu, 19 Nov 1981 08:52:00 GMT"
        ["pragma"]=>
        string(8) "no-cache"
        ["x-robots-tag"]=>
        string(7) "noindex"
        ["link"]=>
        string(65) "; rel="https://api.w.org/""
        ["x-content-type-options"]=>
        string(7) "nosniff"
        ["access-control-expose-headers"]=>
        string(27) "X-WP-Total, X-WP-TotalPages"
        ["access-control-allow-headers"]=>
        string(27) "Authorization, Content-Type"
        ["allow"]=>
        string(3) "GET"
        ["x-cacheable"]=>
        string(5) "SHORT"
        ["vary"]=>
        string(22) "Accept-Encoding,Cookie"
        ["cache-control"]=>
        string(28) "max-age=600, must-revalidate"
        ["accept-ranges"]=>
        string(5) "bytes"
        ["x-cache"]=>
        string(6) "HIT: 1"
        ["x-pass-why"]=>
        string(0) ""
        ["x-cache-group"]=>
        string(6) "normal"
        ["x-type"]=>
        string(7) "default"
      }
    }

Ideally I need both populated, but I could make do with one. I assumed WordPress calculated/populated these via the controller. Do I have to populate them manually? Have I done something incorrectly? Am I experiencing a bug?

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Custom endpoint and X-WP-TotalPages (headers)

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.