WP REST API V2 - Retrieve sub page by full slug (URL/Path)
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...