简体   繁体   English

Wordpress API 获取所有帖子

[英]Wordpress API get all posts

i am trying to get all posts ( only their link ) with wp-json.我正在尝试使用 wp-json 获取所有帖子(只有他们的链接)。 I have around 40 posts currently.我目前有大约 40 个帖子。

this request works partially...it gives me around 8 posts back but not all.这个请求部分有效……它给了我大约 8 个帖子,但不是全部。

/wp-json/wp/v2/posts?_fields=link

when I add all categories to the request I get every post当我将所有类别添加到请求中时,我会收到每个帖子

/wp-json/wp/v2/posts?_fields=link&per_page=100&page=1&categories=1,2,3,6,7,8,21

this solution is not the best since I ll add categories in the future.这个解决方案不是最好的,因为我将来会添加类别。

what is wrong with my first request??我的第一个请求有什么问题?

thank you谢谢你

Use per_page.使用每页。

Search takes per_page /wp-json/wp/v2/search?per_page=40搜索需要 per_page /wp-json/wp/v2/search?per_page=40

See https://developer.wordpress.org/rest-api/reference/search-results/请参阅https://developer.wordpress.org/rest-api/reference/search-results/

Posts will get that many plus revisions.帖子会得到那么多加修订。 per_page should work though. per_page 应该可以工作。 If refreshing in the browser make sure to clear cache each time/ /wp-json/wp/v2/posts?per_page=3如果在浏览器中刷新,请确保每次都清除缓存//wp-json/wp/v2/posts?per_page=3

See https://developer.wordpress.org/rest-api/reference/posts/请参阅https://developer.wordpress.org/rest-api/reference/posts/

You can also use the filter to change the parameters.您还可以使用过滤器更改参数。

add_action( 'rest_movies_params', function( $params ){
    if ( isset( $params ) AND isset( $params[ 'per_page' ] ) ) {
        $params[ 'per_page' ][ 'maximum' ] = 200;
    }
    return $params;
});

Here I have shared the link for official code reference.这里我分享了官方代码参考的链接。 Link 关联

And in fetch url add ?per_page=200并在 fetch url 添加?per_page=200

Here I have shared the example of url:这里我分享了url的例子:

https://example.com/wp-json/wp/v2/movies?per_page=200

Notes:笔记:

  1. Currently in my example (code and url) I have added movies custom post type for reference.目前在我的示例(代码和网址)中,我添加了电影自定义帖子类型以供参考。 you can change according to your requirement.您可以根据您的要求进行更改。
  2. If posts is greter then 200 then you can use the ?page=2 in your url.如果帖子数大于 200,那么您可以在 url 中使用?page=2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM