简体   繁体   中英

Wordpress API get all posts

i am trying to get all posts ( only their link ) with wp-json. I have around 40 posts currently.

this request works partially...it gives me around 8 posts back but not all.

/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

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

Posts will get that many plus revisions. per_page should work though. If refreshing in the browser make sure to clear cache each time/ /wp-json/wp/v2/posts?per_page=3

See 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

Here I have shared the example of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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