@@ -257,6 +257,9 @@ class WordPress {
257257 ///
258258 /// [fetchAll] will make as many API requests as is needed to get all posts.
259259 /// This may take a while.
260+ ///
261+ /// Specify any custom fields in [customFieldNames] . They will be loaded into
262+ /// [Post.customFields]
260263 ///
261264 /// In case of an error, a [WordPressError] object is thrown.
262265 Future <List <Post >> fetchPosts ({
@@ -271,6 +274,7 @@ class WordPress {
271274 bool fetchAttachments = false ,
272275 String postType = "posts" ,
273276 bool fetchAll = false ,
277+ Set <String >? customFieldNames = null
274278 }) async {
275279 if (fetchAll) {
276280 postParams = postParams.copyWith (perPage: 100 );
@@ -288,9 +292,16 @@ class WordPress {
288292 List <Post > posts = [];
289293 final list = json.decode (response.body);
290294
291- for (final post in list) {
295+ for (final Map <String , dynamic > post in list) {
296+ Map <String , dynamic >? customFields;
297+
298+ if (customFieldNames != null && customFieldNames.isNotEmpty) {
299+ customFields = Map .fromEntries (
300+ customFieldNames.map ((key) => MapEntry (key, post[key])));
301+ }
302+
292303 posts.add (await _postBuilder (
293- post: Post .fromJson (post),
304+ post: Post .fromJson (post)..customFields = customFields ?? {} ,
294305 setAuthor: fetchAuthor,
295306 setComments: fetchComments,
296307 orderComments: orderComments,
0 commit comments