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:
https://peteschuster.com/wp-json/wp/v2/posts?slug=high-praise-react-react-ecosystem
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…