@@ -243,6 +243,9 @@ class WordPress {
243243 ///
244244 /// (**Note:** *Set only those fetch boolean parameters which you need because
245245 /// the more information to fetch, the longer it will take to return all Posts*)
246+ ///
247+ /// [fetchAll] will make as many API requests as is needed to get all posts.
248+ /// This may take a while.
246249 ///
247250 /// In case of an error, a [WordPressError] object is thrown.
248251 async .Future <List <Post >> fetchPosts ({
@@ -256,8 +259,14 @@ class WordPress {
256259 bool fetchFeaturedMedia = false ,
257260 bool fetchAttachments = false ,
258261 String postType = "posts" ,
262+ bool fetchAll = false
259263 }) async {
260- final StringBuffer url = new StringBuffer (_baseUrl + URL_WP_BASE + "/" + postType);
264+ if (fetchAll) {
265+ postParams = postParams.copyWith (perPage: 100 );
266+ }
267+
268+ final StringBuffer url =
269+ new StringBuffer (_baseUrl + URL_WP_BASE + "/" + postType);
261270
262271 url.write (postParams.toString ());
263272
@@ -280,6 +289,25 @@ class WordPress {
280289 setAttachments: fetchAttachments,
281290 ));
282291 }
292+
293+ if (fetchAll && response.headers["x-wp-totalpages" ] != null ) {
294+ final totalPages = int .parse (response.headers["x-wp-totalpages" ]);
295+
296+ for (int i = postParams.pageNum + 1 ; i <= totalPages; ++ i) {
297+ posts.addAll (await fetchPosts (
298+ postParams: postParams.copyWith (pageNum: i),
299+ fetchAuthor: fetchAuthor,
300+ fetchComments: fetchComments,
301+ orderComments: orderComments,
302+ orderCommentsBy: orderCommentsBy,
303+ fetchCategories: fetchCategories,
304+ fetchTags: fetchTags,
305+ fetchFeaturedMedia: fetchFeaturedMedia,
306+ fetchAttachments: fetchAttachments,
307+ ));
308+ }
309+ }
310+
283311 return posts;
284312 } else {
285313 try {
@@ -534,7 +562,11 @@ class WordPress {
534562 ///
535563 /// In case of an error, a [WordPressError] object is thrown.
536564 async .Future <List <Category >> fetchCategories (
537- {@required ParamsCategoryList params}) async {
565+ {@required ParamsCategoryList params, bool fetchAll = false }) async {
566+ if (fetchAll) {
567+ params = params.copyWith (perPage: 100 );
568+ }
569+
538570 final StringBuffer url = new StringBuffer (_baseUrl + URL_CATEGORIES );
539571
540572 url.write (params.toString ());
@@ -547,6 +579,16 @@ class WordPress {
547579 list.forEach ((category) {
548580 categories.add (Category .fromJson (category));
549581 });
582+
583+ if (fetchAll && response.headers["x-wp-totalpages" ] != null ) {
584+ final totalPages = int .parse (response.headers["x-wp-totalpages" ]);
585+
586+ for (int i = params.pageNum + 1 ; i <= totalPages; ++ i) {
587+ categories.addAll (await fetchCategories (
588+ params: params.copyWith (pageNum: i)));
589+ }
590+ }
591+
550592 return categories;
551593 } else {
552594 try {
@@ -652,7 +694,7 @@ class WordPress {
652694 }
653695
654696// yahya - @mymakarim
655-
697+
656698 async .Future <dynamic > uploadMedia (File image) async {
657699 final StringBuffer url = new StringBuffer (_baseUrl + URL_MEDIA );
658700 var file = image.readAsBytesSync ();
@@ -678,7 +720,7 @@ class WordPress {
678720 }
679721 }
680722 }
681-
723+
682724// uploadMedia function added by: @GarvMaggu
683725
684726 async .Future <bool > createUser ({@required User user}) async {
0 commit comments