admin管理员组

文章数量:1415111

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
  • 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 Commented Aug 23, 2019 at 0:26
  • 1 This worked! Sally put it in the answer and I'll accept – John Dee Commented Aug 23, 2019 at 1:28
Add a comment  | 

1 Answer 1

Reset to default 3

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/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.

本文标签: rest apiGet a remote post ID via API given URL