Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to use custom fields in posts #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/flutter_wordpress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ class WordPress {
///
/// [fetchAll] will make as many API requests as is needed to get all posts.
/// This may take a while.
///
/// Specify any custom fields in [customFieldNames]. They will be loaded into
/// [Post.customFields]
///
/// In case of an error, a [WordPressError] object is thrown.
Future<List<Post>> fetchPosts({
Expand All @@ -271,6 +274,7 @@ class WordPress {
bool fetchAttachments = false,
String postType = "posts",
bool fetchAll = false,
Set<String>? customFieldNames = null
}) async {
if (fetchAll) {
postParams = postParams.copyWith(perPage: 100);
Expand All @@ -288,9 +292,16 @@ class WordPress {
List<Post> posts = [];
final list = json.decode(response.body);

for (final post in list) {
for (final Map<String, dynamic> post in list) {
Map<String, dynamic>? customFields;

if (customFieldNames != null && customFieldNames.isNotEmpty) {
customFields = Map.fromEntries(
customFieldNames.map((key) => MapEntry(key, post[key])));
}

posts.add(await _postBuilder(
post: Post.fromJson(post),
post: Post.fromJson(post)..customFields = customFields ?? {},
setAuthor: fetchAuthor,
setComments: fetchComments,
orderComments: orderComments,
Expand All @@ -316,6 +327,8 @@ class WordPress {
fetchTags: fetchTags,
fetchFeaturedMedia: fetchFeaturedMedia,
fetchAttachments: fetchAttachments,
customFieldNames: customFieldNames,
postType: postType
));
}
}
Expand Down
11 changes: 11 additions & 0 deletions lib/schemas/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class Post {
/// The featured Media of the post.
Media? featuredMedia;

/// Custom fields on a post.
Map<String, dynamic>? customFields;

Post({
this.date,
this.dateGmt,
Expand All @@ -118,6 +121,7 @@ class Post {
this.format = PostFormat.standard,
this.categoryIDs,
this.tagIDs,
this.customFields,
}) : this.title = new Title(rendered: title),
this.featuredMedia = new Media(sourceUrl: featuredMedia),
this.content = new Content(rendered: content),
Expand Down Expand Up @@ -204,6 +208,13 @@ class Post {
data['categories'] = listToUrlString(this.categoryIDs ?? []);
data['tags'] = listToUrlString(this.tagIDs ?? []);

if (customFields != null) {
for (final key in customFields!.keys) {
data[key] = customFields![key];
}
}


return data;
}

Expand Down