Filter BlogPosts by categories in controller

Silverstripe Version: 4.*

Is there a simple way to filter BlogPosts in the controller?
Something like this:

public function getBlogPosts()
return BlogPost::get()->filter([
                'ParentID' => $blog->ID,
                'Category' => 'Some category']);

Yes… something like that :slight_smile:

What results are you getting when you try? Are they not what you’re expecting?

Well yes and no :slight_smile:
I get "Unknown column 'Category' in 'where clause'" error.

I think I got it:

return BlogPost::get()->filter([
                'ParentID' => $blog->ID,
                'Categories.Title' => 'Title of my category']);

I didn’t know I could use this in filter :slight_smile:

1 Like

Glad you got it sorted :slight_smile:

1 Like