Created
March 31, 2017 08:49
-
-
Save Snaver/9abf24132b53b9d670bb96ab63c11e97 to your computer and use it in GitHub Desktop.
WP Rest API - Retrieve sub page by full slug (URL/Path)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (strstr($slug, '/')) { | |
$segments = explode('/', $slug); | |
$parent_id = null; | |
foreach ($segments as $key => $segment) { | |
$params['slug'] = $segment; | |
if ($parent_id) { | |
$params['parent'] = $parent_id; | |
} | |
$page = $this->client->pages()->get(null, $params); | |
if (empty($page)) { | |
return []; | |
} | |
$parent_id = $page->id; | |
} | |
return $page; | |
} else { | |
$func = $this->client->pages(); | |
if ($id) { | |
return $func->get($id); | |
} else { | |
return $func->get(null, ['slug' => $slug]); | |
} | |
} |
I've crafted a few different solutions as an answer to this WPSE question. It was a really interesting problem to explore :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey! I know its been a long time but I'm asking the same question you did a long time ago - how can I use the WP REST API to filter by a full slug? for example wp/v2/pages?slug=parent/child/grandchild
Any chance you could point me in the right direction?