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 |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.
本文标签: rest apiGet a remote post ID via API given URL
版权声明:本文标题:rest api - Get a remote post ID via API given URL 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745222961a2648479.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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