Support » Plugin: Relevanssi - A Better Search » [Plugin: Relevanssi – A Better Search] Feature Request: "Index content" filter

  • I’m trying to develop a basic plugin for fully integrating WP e-Commerce and Relevanssi, and a feature currently missing that’d make that possible would be a filter within relevanssi_index_doc function to add custom contents to a posts index.

    That’s because WPEC has products (let’s say, “shirt”), which are a custom post type, as well as variations of a product (for example, “white”, “red”, “big”, “small” etc.), each of which is itself a custom post too, but which shouldn’t be accessed directly. So, while the relevanssi_post_ok filter allows me to make sure those variations don’t appear in the search results, there’s currently no direct way for me to make terms specific to the variations become “part” of the main product indexed so that ‘AND’ searching for, let’s say, “red shirt” will find the “shirt” product, or place it above other shirts which don’t have “red” in their text when ‘OR’ searching.

    So, my suggestion is for something like this to be added at line 2334 (i.e., at the content level, after custom fields, and before the excerpt):

    // Allows plugins to add content to a post's index. Must return a string.
    $post->post_content .= ' ' . apply_filters('relevanssi_index_content', $post);

    What do you think?

    By the way: I’m not using product tags, but I noticed product categories don’t seem to be indexed in Relevanssi, so it’s probably the case that WPEC is using something non-standard for its categories and hence that a similar relevanssi_index_taxonomy filter would be needed to make it work. Once I’ve figured the best place (and way) to have it added I’ll post an updated suggestion here. 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Mikko Saari


    Thanks. Sounds like a good suggestion. I’ll add this to the next version of Relevanssi.


    By the way, I’ve been experimenting with this in the last few days, and it works quite nicely. Here’s the setup that I found to be the most flexible:

    At the top (I’m not quite fluent with filters, so maybe the priority should be other than 1?):

    add_filter('relevanssi_index_content', 'relevanssi_default_index_content',1,2);

    Within the relevanssi_index_doc function, at line 2334 (2335 with the above line added):

    $post->post_content = apply_filters('relevanssi_index_content', $post->post_content, $post);

    And after the relevanssi_index_doc function:

    function relevanssi_default_index_content($content, $post) {
    	return $content;

    I hope this helps!

    Now, to attempt the same with categories.

    Plugin Author Mikko Saari


    Priority doesn’t matter as long as there’s just one action on the hook.

    You should rename the filter, though, because there’s already a “relevanssi_index_content” filter used for other purposes.

    Let’s call it relevanssi_content_to_index. I think I’d prefer to do it so that a default function is not necessary. Your initial approach seemed better to me.

    It makes sense. But even if it’s changed to a .= rather than a =, I think there’s the need for a $content parameter so that multiple functions attached to the filter can take what the previous one did and add to it. Third solution then, with some minor sanitization added for good measure, and no need for a default filter:

    $additional_content = trim(apply_filters('relevanssi_content_to_index', '', $post));
    if ('' != $additional_content)
    	$post->post_content .= ' '.$additional_content;

    I’ve tested this and it works perfectly.

    By the way: I think it’d be a good idea to add an observation in the help for this filter mentioning functions attaching to it should always add a space character to the beginning of their returned contents, otherwise the result of the chain might become something like “function 1 textfunction 2 textfunction 3 text”, and not the expected “function 1 text function 2 text function 3 text”.

    Plugin Author Mikko Saari


    Thanks, that makes sense.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Plugin: Relevanssi – A Better Search] Feature Request: "Index content" filter’ is closed to new replies.