@@ -246,6 +246,9 @@ class WordPress {
246246 ///
247247 /// [fetchAll] will make as many API requests as is needed to get all posts.
248248 /// This may take a while.
249+ ///
250+ /// Specify any custom fields in [customFieldNames] . They will be loaded into
251+ /// [Post.customFields]
249252 ///
250253 /// In case of an error, a [WordPressError] object is thrown.
251254 async .Future <List <Post >> fetchPosts ({
@@ -259,7 +262,8 @@ class WordPress {
259262 bool fetchFeaturedMedia = false ,
260263 bool fetchAttachments = false ,
261264 String postType = "posts" ,
262- bool fetchAll = false
265+ bool fetchAll = false ,
266+ Set <String > customFieldNames = null
263267 }) async {
264268 if (fetchAll) {
265269 postParams = postParams.copyWith (perPage: 100 );
@@ -276,9 +280,16 @@ class WordPress {
276280 List <Post > posts = new List ();
277281 final list = json.decode (response.body);
278282
279- for (final post in list) {
283+ for (final Map <String , dynamic > post in list) {
284+ Map <String , dynamic > customFields;
285+
286+ if (customFieldNames? .isNotEmpty ?? false ) {
287+ customFields = Map .fromEntries (
288+ customFieldNames.map ((key) => MapEntry (key, post[key])));
289+ }
290+
280291 posts.add (await _postBuilder (
281- post: Post .fromJson (post),
292+ post: Post .fromJson (post)..customFields = customFields ,
282293 setAuthor: fetchAuthor,
283294 setComments: fetchComments,
284295 orderComments: orderComments,
0 commit comments