I am trying to figure out a way to search through posts’ custom fields. Basically, what I need is to find a post where post.CustomField1 == “some value”
I’ve searched and searched and been digging through the Graffiti CMS source code (graffiticms.codeplex.com) and can’t figure out how I would do this.
As EJB said, the solution varies with where you want to implement the search.
If you want to find a post with a particular custom field value, searching on just the posts displayed on the current page (such as index.view or a category view) you could do it with Chalk in a template like this:
You could also use the API to iterate through ALL posts and check for the custom value. Unfortunately Graffiti CMS doesn’t have a built-in method to query the database for posts based on a specific custom field value.
However you could use the built-in Lucene-based search engine. If you want to enable searching for a particular custom field value using search, you’d need to make a couple tweaks to the source code in the Graffiti.Core.SearchIndex class. In the CreateDocument method add the custom field value to the indexed Document like this:
Then in the GetQueryParser method add that key to the list of fields to search on:
With the two changes above, you’d be able to do a search for “some value” and have it return any posts with CustomField1 value of that.
Hope that helps!