0

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?

  • If you know the slug (based on the URL), then you can use the 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 Aug 23 at 0:26
  • 1
    This worked! Sally put it in the answer and I'll accept – John Dee Aug 23 at 1:28
2

The 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.com/hello-world/ and you know the slug is hello-world, then for example in JavaScript, you can do something like:

fetch( 'http://example.com/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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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