Given you have a URL, how can you get the post ID of a post on a remote WordPress site?
As far as I can tell, you would have to:
HTTP GET /wp-json/v2/posts
And then look for your URL, but the problem is that this endpoint will only return 100 results by default.
I don't want to have to download the ENTIRE content of a site just to find the post ID of a given URL. In V1 you could submit a query, but I think that's been depreciated. Any better ways to do this?
Given you have a URL, how can you get the post ID of a post on a remote WordPress site?
As far as I can tell, you would have to:
HTTP GET /wp-json/v2/posts
And then look for your URL, but the problem is that this endpoint will only return 100 results by default.
I don't want to have to download the ENTIRE content of a site just to find the post ID of a given URL. In V1 you could submit a query, but I think that's been depreciated. Any better ways to do this?
Share Improve this question asked Aug 22, 2019 at 23:27 John DeeJohn Dee 5135 silver badges14 bronze badges 2 |1 Answer
Reset to default 3The Posts endpoint accepts a slug
parameter when querying for posts, so if you can get the slug from the URL, then you can make request to /wp-json/wp/v2/posts?slug=<slug>
.
So if the URL is http://example/hello-world/
and you know the slug is hello-world
, then for example in JavaScript, you can do something like:
fetch( 'http://example/wp-json/wp/v2/posts?slug=hello-world' )
.then( res => res.json() )
.then( posts => console.log( posts[0].id ) );
Or you could create a custom endpoint.. if you've got control over the WordPress site. See this for more details.
slug
parameter -/wp-json/wp/v2/posts?slug=hello-world
. Or you could create a custom endpoint.. if you've got control over the WordPress site. – Sally CJ Commented Aug 23, 2019 at 0:26