Filter Recent Blog Posts by Category

Eric Conklin asked on August 26, 2020 19:16

I am using a transformer to get a list of recent posts. I want to filter these posts by a category ID value that I can get from the route parameters.

<h4><a href="<%# GetDocumentUrl() %>"><%# Eval("BlogPostTitle", true) %></a><br/></h4>

<div class="listBoxWithTeaser">
<div class="teaser"><%# IfEmpty(Eval("BlogPostTeaser"), "<a href=\""+GetDocumentUrl()+"\" title=\""+HTMLEncode(LimitLength(Eval("BlogName"), 70, "..."))+"\"><img src=\"~/App_Themes/CorporateSite/Images/no_image.png\" width=\"90\" alt=\"Default blog teaser image\" /></a>", "<a href=\""+GetDocumentUrl()+"\" title=\""+HTMLEncode(LimitLength(Eval("BlogName"), 70, "..."))+"\">"+GetImage("BlogPostTeaser", 250)+"</a>") %></div>
<div class="description">
<p><%# Eval("BlogPostSummary") %> <br>
  <a class="learn-more" href = "<%# GetDocumentUrl() %>">Learn More</a>

  <br /><br />
<span class="bold black"><%# BlogFunctions.GetUserFullName(Eval("NodeOwner")) %></span> |
<%# Eval("BlogPostDate") %> |
<a class="gray" href="<%# GetDocumentUrl() %>#comments"><%# BlogFunctions.GetBlogCommentsCount(Eval("DocumentID"), Eval("NodeAliasPath")) %> comments</a>

Can someone suggest how to filter these objects within this transformer?

Recent Answers

Brian McKeiver answered on August 26, 2020 21:56 (last edited on August 26, 2020 21:56)

You would normally filter out items in a list within a datasource webpart or repeater webpart that you are using. The transformation should really only be your presentation layer.

Most likely you have a repeater though. You can add into the WHERE Condition property something like this:

('{%ToInt(tagid, "")%}' = 0 AND '{%tagname%}'='') OR (DocumentID IN (SELECT DocumentID FROM CMS_DocumentTag WHERE TagID = {% ToInt(tagid) %}))

That filters our blog posts by tags.

{%tagid%} is a query string macro to pull in the variable.

If you are passing around a CategoryID value in the URL can modify from tagid to CategoryID but you would have to change the SQL WHERE clause to look at Document Category not DocumentTag

1 votesVote for this answer Mark as a Correct answer

Kevin Casino answered on April 7, 2021 10:04

Very helpfull, really thanks sir

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.