There's two solutions, depending on how you're rendering the Documents.
1: If you are using a Repeater, you need to write in the WHERE statement to filter out any item where the DocumentID is in the Document/Category Table for those categories. Below is a custom macro i made to do that.
2: If you are using a Smart Search Index, you need to add the Category Names to the Smart Search Index manually (make sure no numbers in the Category Name so it doesn't split them upon tokenizing), and then add a filter to filter on those. You can use the built in Kentico Filters to render something like +Categories('blue', 'short')
Macro to generate WHERE Items for Categories (used in WHERE area like:
(CategoryType is null or {% Util.FilterByCategories(EventTypes, "ANY", "|", false) |(handlesqlinjection)false|(identity)GlobalAdministrator%}
)
using CMS.Base;
using CMS.DocumentEngine;
using CMS.Membership;
using System.Collections.Generic;
[SmartSearchContentLoader]
public partial class CMSModuleLoader
{
/// <summary>
/// Attribute class for assigning event handlers.
/// </summary>
private class SmartSearchContentLoaderAttribute : CMSLoaderAttribute
{
/// <summary>
/// Called automatically when the application starts.
/// </summary>
public override void Init()
{
// Assigns a handler to the GetContent event for pages
DocumentEvents.GetContent.Execute += OnGetPageContent;
}
private void OnGetPageContent(object sender, DocumentSearchEventArgs e)
{
// Gets an object representing the page that is being indexed
TreeNode indexedPage = e.Node;
// Checks that the page exists
if (indexedPage != null)
{
// Add category names to a searchable field "Categories" for filtering / weighing.
List<string> joinedCategories = new List<string>();
foreach (CMS.Taxonomy.CategoryInfo cat in e.Node.Categories)
{
joinedCategories.Add(cat.CategoryName);
}
e.SearchDocument.AddGeneralField("Categories", string.Join("|", joinedCategories.ToArray()), true, true);
// Gets the user object of the page owner
UserInfo pageOwner = UserInfoProvider.GetUserInfo(indexedPage.NodeOwner);
if (pageOwner != null)
{
// Adds the value of the "Description" field from the owner's user settings into the indexed content
// Spaces added as separators to ensure that typical search index analyzers can correctly tokenize the index content
//e.Content += " " + pageOwner.UserDescription + " ";
}
}
}
}
}
Adding Custom Categories to Smart Search Index:
using CMS.Base;
using CMS.DocumentEngine;
using CMS.Membership;
using System.Collections.Generic;
[SmartSearchContentLoader]
public partial class CMSModuleLoader
{
/// <summary>
/// Attribute class for assigning event handlers.
/// </summary>
private class SmartSearchContentLoaderAttribute : CMSLoaderAttribute
{
/// <summary>
/// Called automatically when the application starts.
/// </summary>
public override void Init()
{
// Assigns a handler to the GetContent event for pages
DocumentEvents.GetContent.Execute += OnGetPageContent;
}
private void OnGetPageContent(object sender, DocumentSearchEventArgs e)
{
// Gets an object representing the page that is being indexed
TreeNode indexedPage = e.Node;
// Checks that the page exists
if (indexedPage != null)
{
// Add category names to a searchable field "Categories" for filtering / weighing.
List<string> joinedCategories = new List<string>();
foreach (CMS.Taxonomy.CategoryInfo cat in e.Node.Categories)
{
joinedCategories.Add(cat.CategoryName);
}
e.SearchDocument.AddGeneralField("Categories", string.Join("|", joinedCategories.ToArray()), true, true);
}
}
}
}