WordPress.org

Ready to get started?Download WordPress

Forums

Relevanssi - A Better Search
Snippets not created from Custom Fields? (37 posts)

  1. Chee Studio
    Member
    Posted 3 years ago #

    Hi there...first of all, LOVE this plugin, absolutely awesome to work with right out of the box.

    Everything is working great for me using some Custom Post Types and Custom Fields (which were created using Custom Field Template plugin).

    The search results are returned when there is a hit within the custom field of my post types, but the only problem is there is no snippet! Snippets work for regular posts (naturally) and for page results, but not for custom fields, I just get the infamous three dots.

    I know its finding the individual words because when I use %total%, I can see how many hits it saw. But it won't create the snippet (Custom Snippets is turned on).

    Any idea why this could be?

  2. Chee Studio
    Member
    Posted 3 years ago #

    Here is a screenshot if need be:

    http://goodchee.com/econest-search.jpg

  3. Mikko Saari
    Member
    Plugin Author

    Posted 3 years ago #

    Relevanssi doesn't build excerpts from custom fields. It's not a bug.

    However, not getting an excerpt at all is a bug. When I try this at my test blog, I just get an excerpt of the post.

    What's in your search results template? A call to the_excerpt()? I checked at your site and the Nesting Instinct entry that's causing trouble does have the words "clay straw" in the post content, so it should definitely be highlighted...

    Well, it's hard to say what's the problem without taking a closer look under the hood.

  4. Chee Studio
    Member
    Posted 3 years ago #

    Interesting...well, here is what I have in my search template:

    [Code moderated as per the Forum Rules. Please use the pastebin]

  5. Chee Studio
    Member
    Posted 3 years ago #

    Also, to give you a little more information:

    One of the Custom Fields I have is called 'Description'. If I call that into the Search Template, hoping that it would just return the highlighted portion, it returns the entire custom field, rather than a snippet.

    Just thought I'd mention that. I didn't expect it to work, but just in case you might suggest it.

  6. Mikko Saari
    Member
    Plugin Author

    Posted 3 years ago #

    Relevanssi custom snippets work through the post excerpt. If you enable custom snippets, Relevanssi will go through the search results, create new excerpts and replace the post excerpts with the new excerpts.

    So, only way to get the custom excerpts visible is to use excerpts, either with the_excerpt() or relevanssi_the_excerpt().

  7. Chee Studio
    Member
    Posted 3 years ago #

    Hmm...so does this mean it should be working? Not sure if my code came through, but here is the Pastebin:

    http://pastebin.com/gcF6NWDM

    I also tried using relevanssi_the_excerpt(), no go either.

    But from what I gather from your posts, if the search result exists within a Custom Field, it will not build or display an excerpt or snippet of any kind, even if I use relevanssi_the_excerpt()?

  8. Mikko Saari
    Member
    Plugin Author

    Posted 3 years ago #

    If the search result exists only within a custom field, Relevanssi should build a default excerpt, which is taken from the beginning of the entry. At least that's what it does on every blog I have.

    So, unfortunately I don't have a clue what's going on here, and just looking at the results isn't going to help much. There's something in your blog that's throwing the Relevanssi excerpt creation off the tracks and figuring it out would take some closer debugging.

  9. Chee Studio
    Member
    Posted 3 years ago #

    Well I tried this same technique on another site and same thing occurs...I'm wondering if it possibly has to do with that I am using Custom Field Template to create my custom fields, since that's the only plugin that these two sites have in common.

    What do you use when you create your custom fields?

  10. Mikko Saari
    Member
    Plugin Author

    Posted 3 years ago #

    I always do them manually.

  11. davidnde
    Member
    Posted 3 years ago #

    Hi guys.

    I was having the same problem, the excerpt created by relevanssi does not contain any content from custom fields. This is my workaround:

    Let's say you are in the following situation. You have a post-type my-custom-post-type (or post or page) that is extended with custom fields (aka meta-data) myfield1 and myfield2. We want the content of those field be contained in the excerpt generated by relevanssi. Since the the_content hook is used by relevanssi to get the content it creates the excerpt from, we need to implement this hook, either in you functions.php or in you plugin file (Make sure to replace my-custom-post-type, myfield1, myfield2 and functionnames in the following code):

    [Code moderated as per the Forum Rules. Please use the pastebin]

    This is not the nicest hack but it works perfectly. I would request the following feature from the developers: Register a custom hook for this purpose so we don't need backtracing and make the $post object available as you would expect it when the_content is triggered.

  12. Chee Studio
    Member
    Posted 3 years ago #

    Awesome, Davidn! Would you be able to create a pastebin so I could see the code you are using?

  13. Mikko Saari
    Member
    Plugin Author

    Posted 3 years ago #

    Ah, good idea davidn.de - I'm always in favour of creating useful filter hooks. I've got 2.9.1 almost complete, I'll squeeze this in.

  14. davidnde
    Member
    Posted 3 years ago #

    Ah thank you moderator, it's all gone. Never mind, I mist this rule (but why is there a code tag in this form???)

    Here we go: http://pastebin.com/Zx5qx4kN

  15. esmi
    Forum Moderator
    Posted 3 years ago #

  16. Chee Studio
    Member
    Posted 3 years ago #

    Hey David, I'm having some issues getting this filter to work correctly (I keep getting invalid arguments when it tries to implode).

    If its not too much trouble, can you show me a pastebin of what it should look like if my Post Type is "Article" and the custom field is "Article Text"?

    I so greatly appreciate this!

  17. davidnde
    Member
    Posted 3 years ago #

    Chee, you are right, my code only worked if values are arrays. Try this again:
    http://pastebin.com/wdvPFQyd

    I just changed

    $content .= ' '. implode(' ', get_post_meta($post->ID, $field, TRUE));

    to

    $field_value = get_post_meta($post->ID, $field, TRUE);
          $content .= ' '. ( is_array($field_value) ? implode(' ', $field_value) : $field_value );

    If you don't get along with this:
    What do you mean with "Articel"? Do you mean the wordpress core post-type post, or you custom post-type named Articel?

  18. davidnde
    Member
    Posted 3 years ago #

    BTW: If you are storing arrays as meta-data, relevanssi's indexing will not work:
    http://wordpress.org/support/topic/plugin-relevanssi-a-better-search-bug-report-wrong-use-of-get_post_meta?replies=1

  19. Chee Studio
    Member
    Posted 3 years ago #

    I'll give that a shot.

    I meant a custom post type of Article, with a custom field called "Article Text."

  20. Chee Studio
    Member
    Posted 3 years ago #

    Awesome, that worked for me!

    One last question...how would I go about passing multiple post types to this filter along with their corresponding custom fields? Or do I need to repeat this filter for each set of post types/custom fields?

  21. davidnde
    Member
    Posted 3 years ago #

    Nice!
    Actully the code I posted did not restrict to this one post type (Or did you have to type the post-type anywhere?). Just fill $fields with any custom field you want to show up in relevanssi's excerpt.

    This will work even though there is no single post-type that has all the custom fields.

    BTW: Checking for post-types and only doing this is_the_content_called_by_relevanssi trick for post-types that have custom fields increases speed a little bit...

  22. Mikko Saari
    Member
    Plugin Author

    Posted 3 years ago #

    Version 2.9.1 will have a filter "relevanssi_excerpt_content" that passes three parameters: $content right after 'the_content' is called but before any cleanup, the whole $post object and the search query.

    Then just do something like this:

    add_filter('relevanssi_excerpt_content', 'excerpt_function', 10, 3);
    function excerpt_function($content, $post, $query) {
            //add whatever you want to $content here
    	return $content;
    }

    I'm releasing 2.9.1 today.

  23. davidnde
    Member
    Posted 3 years ago #

    Super! I upgraded my plugin and it works perfectly fine. For those who like to use this brand new feature, here is the code.

  24. Mikko Saari
    Member
    Plugin Author

    Posted 3 years ago #

    Very nice bit of code, can I post it in the Relevanssi knowledge base? I'll give you credit and if you have a website you want me to link to, just let me know.

  25. Chee Studio
    Member
    Posted 3 years ago #

    Well I had this working but for some reason when I upgraded and then used the code Davidn posted, I get:

    Warning: Missing argument 2 for my_relevanssi_excerpt_content()

    Its in reference to this line:

    function my_relevanssi_excerpt_content($content, $post, $query)

    Am I supposed to modify this in any way?

    Sorry to be a pain. I'm still learning PHP the hard way (trial and error).

  26. Mikko Saari
    Member
    Plugin Author

    Posted 3 years ago #

    Change

    add_filter('relevanssi_excerpt_content', 'my_relevanssi_excerpt_content');

    to

    add_filter('relevanssi_excerpt_content', 'my_relevanssi_excerpt_content', 10, 3);

    and it should work.

  27. Chee Studio
    Member
    Posted 3 years ago #

    Interesting...that did stop the errors, but now instead of returning the actual excerpts at all (including ones not created from custom fields), all I get is:

    "content..."

  28. Mikko Saari
    Member
    Plugin Author

    Posted 3 years ago #

    You probably have a mistake in your code... Ah, noticed. The last line of the function says return content;, but it should be return $content;.

  29. davidnde
    Member
    Posted 3 years ago #

    Oh, right, The $ was missing. Here is the updated version: http://pastebin.com/w2VXadNd

    You are very welcome to post the code into the knowledge base. If you don't mind, link to http://continent7.com.

  30. davidnde
    Member
    Posted 3 years ago #

    To face @Chee Studio problem, I would rather recommend to set default values in my_relevanssi_excerpt_content(). Here we go with the last updated version:
    http://pastebin.com/cczmZDay

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic