tags:

views:

90

answers:

1

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.

A: 

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:

#foreach($post in $posts)
  #if($post.Custom("CustomField1") == "some value")
    display or do something with $post
  #end
#end

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:

doc.Add(Field.Text("CustomField1", t.Custom("CustomField1") ?? string.Empty));

Then in the GetQueryParser method add that key to the list of fields to search on:

return new MultiFieldQueryParser(new string[] { "body", "title", "CustomField1" }, a);

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!

Kevin Harder
None of these are the exact solution I am looking for, but I think the latter will work. Thanks.
Jeremy H