Search code examples
c#sitefinitysitefinity-8

How to get posts filtered by category in SiteFinity 8


I'm trying to get post information from SiteFinity through my custom widget (ascx control). I need to filter these posts by category. I'm fairly new with SiteFinity and appreciate any input or direction that can be given.

Thanks!


Solution

  • I was able to figure out how to achieve this. Please, see code below:

    BlogsManager blogsManager = BlogsManager.GetManager();
    TaxonomyManager manager = TaxonomyManager.GetManager();
    HierarchicalTaxon taxo = manager.GetTaxa<HierarchicalTaxon>().Where(t => t.Taxonomy.TaxonName == "Category" && t.Name == "YOUR_CATEGORY_NAME").SingleOrDefault();
    System.Linq.IQueryable<BlogPost> blogPosts = blogsManager.GetBlogPosts().Where(b => b.Status == ContentLifecycleStatus.Live && b.GetValue<TrackedList<Guid>>("Category").Contains(taxo.Id));
    
    foreach (BlogPost blogPostObj in blogPosts) {
    //HERE YOU CAN USE BLOG POST INFORMATION
    }