I'm in the process of converting my existing WordPress site to use Preact/Redux on the frontend and use the WP REST API to get post data back from the admin.

I'll be using React Router and a rewrite in .htaccess to rewrite the URLs to the index.html file, and React Router should handle the requests.

Given an existing URL of https://peteschuster.com/2017/10/high-praise-react-react-ecosystem/ I assume I'll setup my route as /:year/:month/:slug

My question is, how would I use those params to get the post ID so I can call the REST endpoint for: GET /wp/v2/posts/<id>?

Perhaps there is another way to achieve this? I tried several combinations of: https://peteschuster.com/wp/v2/posts/2017/10/high-praise-react-react-ecosystem/ to no avail. I was thinking it might be possible similar to how /feed is handled like: https://peteschuster.com/2017/10/high-praise-react-react-ecosystem/json

Any guidance would be greatly appreciated. Thanks!

3 Answers 3


The core rewrite API does offer the function url_to_postid() (as mentioned by @stephen-sabatini) which can find a post ID from a URL. However it seems less than ideal to have to make a request up front just to determine the post ID...

The REST API does not natively offer the ability to query by date beyond setting before and after args so that won't be of much help to you.

You could potentially ignore the date params and query by slug. For example:


Alternatively, you could make a custom endpoint and perform whatever type of query you want. Check the REST API handbook for more information.

On the idea of a /json endpoint: This isn't something that exists natively, but it is certainly doable.

To implement it, have a look at rewrite endpoints:

Keep in mind that this would be completely separate from the REST API, however you could probably reuse the REST API internally with something like:

$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'slug', 'high-praise-react-react-ecosystem' );
$response = rest_do_request( $request );
$post = $response->data;

Using the REST API in this way is briefly touched on in the handbook FAQ.

This would add some extra complexity to your server config, as now you would have to route requests ending in json straight to WordPress instead of your frontend, but that shouldn't be too difficult.

Hopefully that gives you some ideas...

  • thanks, the slug filtering is what I'm going to use. I didn't want to go to the server to retrieve the posts ID only to make another request for the posts data. Thanks for your help Oct 12, 2017 at 19:11

Thought I would add my two cents to this.

using wp-json/wp/v2/posts?slug= works very well until you start working with parent & child pages.

No two pages can have the same slug UNLESS one of the pages is the child off another page. Here's a quick example where I have a page called lorem-2 (the -2 was added automatically cause I already had a paged called lorem).

Then I added a child page called lorem-2 to lorem, then when I query for lorem-2 I get two pages.

A safer rout would be to make a custom REST route that let's you query the whole URL as that will provide you with the exact post/page

enter image description here

  • That's a good point, passing the parent ID will prevent this issue. wp-json/wp/v2/posts?slug=x&parent=y
    – RRikesh
    Oct 17, 2022 at 2:43

Firstly, the REST API URL base would be:


I'm not sure I understand your question as a whole, but if you're trying to get the ID from the post URL, you can use url_to_postid($url);.

See: https://codex.wordpress.org/Function_Reference/url_to_postid

The full URL would be: https://peteschuster.com/wp-json/wp/v2/posts/ID_HERE

e.g. https://peteschuster.com/wp-json/wp/v2/posts/4177

You can also get the post by the slug using a query: (I'd probably recommend)


You can get the slug either by using $post->post_name or basename(get_permalink());.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.