Skip to content

Instantly share code, notes, and snippets.

@Snaver
Created March 31, 2017 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Snaver/9abf24132b53b9d670bb96ab63c11e97 to your computer and use it in GitHub Desktop.
Save Snaver/9abf24132b53b9d670bb96ab63c11e97 to your computer and use it in GitHub Desktop.
WP Rest API - Retrieve sub page by full slug (URL/Path)
<?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]);
}
}
@Spence1115
Copy link

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?

@bosconian-dynamics
Copy link

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