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]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've crafted a few different solutions as an answer to this WPSE question. It was a really interesting problem to explore :)