4

I'm struggling to see if this is natively supported, or if it's not the best place to implement this functionality - in my app (Laravel) or the same side as the API (WordPress).

Retrieving a single page (/about-us) via the slug is easy:

/wp-json/wp/v2/pages?slug=about-us

But the issue comes with retrieving a sub/child page. Consider /about-us/child-page - this works perfectly fine in WordPress but retrieving the page via the Rest API seems impossible?

/wp-json/wp/v2/pages?slug=about-us/child-page
/wp-json/wp/v2/pages?slug=%2Fabout-us%2Fchild-page%2F

No Results.

I can search for that individual page and get results, however if another page then shares that slug there's the potential for collisions.

/wp-json/wp/v2/pages?slug=child-page

There's get_page_by_path() which allows you to search for pages via path which is exactly what I'm after - I can implement this using a custom REST API endpoint (https://www.coditty.com/code/wordpress-rest-api-how-to-get-content-by-slug) but the returned result is not standard and not comparable to the WP-REST equivalent (See below)

{
    "id": 22,
    "date": "2017-03-28T13:15:53",
    "date_gmt": "2017-03-28T12:15:53",
    "guid": {
      "rendered": "http://127.0.0.1:8000/?page_id=22"
    },
    "modified": "2017-03-28T13:15:53",
    "modified_gmt": "2017-03-28T12:15:53",
    "slug": "test-sub-page",
    "status": "publish",
    "type": "page",
    "link": "http://127.0.0.1:8000/test/test-sub-page/",
    "title": {
      "rendered": "Test Sub page"
    },
    "content": {
      "rendered": "...",
      "protected": false
    },
    "excerpt": {
      "rendered": "....",
      "protected": false
    },
    "author": 1,
    "featured_media": 0,
    "parent": 7,
    "menu_order": 0,
    "comment_status": "closed",
    "ping_status": "closed",
    "template": "",
    "meta": [],
    "_links": {
      "self": [
        {
          "href": "http://127.0.0.1:8000/wp-json/wp/v2/pages/22"
        }
      ],
      "collection": [
        {
          "href": "http://127.0.0.1:8000/wp-json/wp/v2/pages"
        }
      ],
      "about": [
        {
          "href": "http://127.0.0.1:8000/wp-json/wp/v2/types/page"
        }
      ],
      "author": [
        {
          "embeddable": true,
          "href": "http://127.0.0.1:8000/wp-json/wp/v2/users/1"
        }
      ],
      "replies": [
        {
          "embeddable": true,
          "href": "http://127.0.0.1:8000/wp-json/wp/v2/comments?post=22"
        }
      ],
      "version-history": [
        {
          "href": "http://127.0.0.1:8000/wp-json/wp/v2/pages/22/revisions"
        }
      ],
      "up": [
        {
          "embeddable": true,
          "href": "http://127.0.0.1:8000/wp-json/wp/v2/pages/7"
        }
      ],
      "wp:attachment": [
        {
          "href": "http://127.0.0.1:8000/wp-json/wp/v2/media?parent=22"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }
}

VS

{
  "ID": 22,
  "post_author": "1",
  "post_date": "2017-03-28 13:15:53",
  "post_date_gmt": "2017-03-28 12:15:53",
  "post_content": "...",
  "post_title": "Test Sub page",
  "post_excerpt": "",
  "post_status": "publish",
  "comment_status": "closed",
  "ping_status": "closed",
  "post_password": "",
  "post_name": "test-sub-page",
  "to_ping": "",
  "pinged": "",
  "post_modified": "2017-03-28 13:15:53",
  "post_modified_gmt": "2017-03-28 12:15:53",
  "post_content_filtered": "",
  "post_parent": 7,
  "guid": "http://127.0.0.1:8000/?page_id=22",
  "menu_order": 0,
  "post_type": "page",
  "post_mime_type": "",
  "comment_count": "0",
  "filter": "raw"
}

Alternatively I can poll the API for each segment/page from my app to verify that each page exists and build up the data that way...

  • Why /wp-json/wp/v2/pages?slug=child-page is a problem ? Slugs are unique, right? Maybe you can try parent argument but it has to be an ID. It seems to me a bit redundant to have /wp-json/wp/v2/pages?slug=about-us/child-page just for the request and not for the url the users sees and will probably share or bookmark. – Laxmana Mar 28 '17 at 16:46
  • Thanks @Laxmana, I ended up with this solution: gist.github.com/Snaver/9abf24132b53b9d670bb96ab63c11e97 – Snaver Mar 31 '17 at 8:50
  • I think in most applications the slug would be about/{me} and contact/{me} returning posts by slug would give you two different pages. I feel like a query by URI or GUID would be more efficient and is unique. – Adam Patterson Jan 27 '18 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.