Silverstripe: Filter blog posts by author

I have been working on the Internet for about 2 hours and cannot find the answer to this question. I am trying to filter blog posts (using the silverstripe-blog module) by / MemberID. So far I have:

public function MyRecentPosts() { $posts = BlogPost::get() ->sort('PublishDate','desc') ->limit(2); return $posts; } 

Obviously, it only returns the latest blog entries. I'm not sure I understand how to link a Blog Post table to a BlogPost_Authors table ...

Any advice is appreciated.

+5
source share
1 answer

Well, BlogMemberExtension applies to the Member class, which provides you with an easy way to access member posts through the many-to-many association.

I assume that this function will not be in the Member extension, and that you will pass the identifier of the element, since it is no longer present in your code. This assumption may not be correct, since your method is called " My RecentPosts", but in any case - here is an example:

 public function MyRecentPosts($memberId) { $member = Member::get()->byId($memberId); $posts = $member->BlogPosts() ->sort('PublishDate', 'desc') ->limit(2); return $posts; } 

You can also do this from the BlogPost model through your many many many association:

 $posts = BlogPost::get() ->filter(array('Authors.ID' => $memberId)) ->sort('PublishDate', 'desc') ->limit(2); 
+7
source

All Articles