If you want to prevent the indexing titles or post content for some reason, Relevanssi has couple of useful filters for that. The filters are called relevanssi_index_content
and relevanssi_index_titles
. These are simple true/false filters, so if you want to activate the filter, add a function that returns false, like this:
add_filter( 'relevanssi_index_content', '__return_false' );
That’s all. Replace relevanssi_index_content
with relevanssi_index_titles
to stop titles (and please don’t stop both at the same time).
Once you’ve got the function in your functions.php
file, you need to reindex the database. If you’ve stopped post content, indexing should be much faster than usual.
If you then remove the function, the effect will remain until you reindex the database (or posts are indexed when they are updated).
w0w, great filter. Seems simpler then the code I’m using 🙂
Bryan
Do i have to upgrade to Relvanssi premium to be able to exclude post titles from my search results? Or can I just set the weight to 0?
Don’t set the weight, use the filter described in this post.
This filter doesnt seem to work. I still have results based off post content. Im running 1.6 Premium.
Have you re-indexed the database after adding the filter?
I want to search only in Post Titles, how can i do so? I’ve set the weight 10 to Post titles and 0 to everything else + i added the filter above for content.
With all this it still searches in content/excerpt although “Create custom search result snippets” is disabled.
I get search results which have no searched word in title but it has in content above search results that have the search keywords in title.
Solution?
Thanks.
If you add the filter, nothing from post content will go to the index. If you have the filter in place, correctly set up, and have indexed the database after that, and you still get hits from post content, your theme is probably stopping Relevanssi from working at all and you’re getting WordPress search results.
How can I exclude images from showing in the search results?
Jason, do you mean attachments? If so, just don’t index them.
I mean images I have in the posts. Is that the same as an attachment? And how do I not index them?
No, that’s not the same. You need to modify your search results template. I don’t know what your search results template is currently showing, so it depends on that, but Relevanssi-generated excerpts should not have images in them.
Mikko, thanks for the info! i’ve just installed and activated Relevanssi, but my search results simply contain a list of each of the pages that match the result. I’ve indexed a couple times so far. How can I get it to show the special snippets and remove images? I found an option for limiting the size of a snippet, but that didn’t work. Is there a different widget I should use, or just the main one? http://www.nationalloopcompany.com Thanks! I just wanna use Relevanssi! 🙂
Yu need to adjust your search results template. The look of the results depends from your theme, Relevanssi doesn’t actually have anything to do with it.
Thanks for the reply! So just to clarify, (because believe it or not this info isn’t in the user guide) we just have to install, activate, build db, and use the standard WordPress widget? Thanks!
Yes. This is explained in both the User Manual and the plugin info page on the WordPress.org repository, though…
is it under the “Basic use and parameters” section? that link isn’t clickable.
I’ve searched this website and the wordpress.org site, but neither seem to have directions that say anything about using the widget. If you do a search for “Widget” you’ll see what i mean. One would think that the directions would have to say “widget” at least once. The directions say nothing about using the widget. Thanks!
It’s mentioned right in the installation instructions: http://wordpress.org/extend/plugins/relevanssi/installation/
Also, if the instructions don’t mention widgets, I think it’s a safe bet to assume there’s no need to do anything about widgets, ie. use the search widget your theme already has…
Thanks for the update! It’s very understandable to feel that way. Obviously, we wouldn’t have had this conversation if you were correct. You can simply write something to the effect of “1. Install, 2. Activate, 3. Build DB, 4. and use the standard widget included with wordpress.” BOOM problem solved. There are a lot of threads out there with people asking the same question as me. I wish you the best of success! one hint… try to assume your users are idiots like me, we neeeeeed detailed instructions or else we waste your time with crap like this 🙂 Best Regards!
There is a way to exclude some blocks of text (in a post or page) from
be indexing and therefore displayed in the search results of Relevanssi?
Yes: just wrap them in the [noindex] shortcode.
Could I have a more detailed explanation of how to use this “noindex” shortcode? Does it work in the free version?
There’s really nothing else to it. [noindex]This text will not be indexed.[/noindex] Yes, it does work in the free version.
I added the above code (but with relevanssi_index_titles) to my functions.php file, the post titles still appear in the search results. Do I need to have the Premium version to get this to work?
Did you reindex the database? The code doesn’t work until you do. You don’t need Premium, this works with the free version just as well.
I added this code to my functions.php file
add_filter(‘relevanssi_index_titles’, ‘contentoff’);
function contentoff() {
return false;
}
then I went to Relevannsi to Build the Index. After doing a search I still see post titles within the excerpts. Are we missing some setting or is there another way to prevent post titles from appearing in the search results?
That code doesn’t affect search results pages. If you don’t want titles on your search results pages, you need to remove them from your search results template.
Can I just past this into my custom css box?
No, you can’t, because this is not CSS.
I really would like to use this plugin but I need to exclude the post content. Is there any simple way for a newbie to do this?
hidemyage.com
Take the code above, add it to the functions.php in your theme, rebuild the index. Done.
Thank you Mikko! You’ve taught me something new! Much appreciated.
A couple of questions…
How can I include featured image thumbnails in my search results?
Can you take a look at my site and tell me if I would benefit from upgrading to the premium version? I don’t have a lot of information on there yet…
Thanks!!
Kim
hidemyage.com
Kim, to add image thumbnails, you need to edit your search results template.
Upgrading to Premium is a great idea, if you need to get user profiles or taxonomy terms in the search results. If you don’t, then the benefits are smaller.
How do I edit my search results template for thumbnails? I’ve looked and don’t see it.
I don’t know, it depends on which theme you are using. If you have a premium theme, ask the theme support.
Why I have to disable the filter relevanssi_index_content if I do not want to. The problem is that when indexing the content, code try to execute shortcodes (in the content), so, whene executed, template functions are not load, so -> FATAL ERROR.
I would like a nice solution for this issue.
Line: lib/indexing.php: 367
(for example, woocommerce shortcode are going to cause a Fatal Error).
You certainly don’t need to disable content indexing unless you don’t want to. See Relevanssi settings: there’s an option to disable shortcode expansion in indexing. Uncheck that option and WooCommerce shortcodes won’t cause problems anymore.
Would this also work in the backend – specifically, in WooCommerce – when creating a new order manually when I add a product I want to stop the search looking in the product description – just the title.
Thanks
This affects all Relevanssi searches.
Thanks – I’ve added it to my functions.php and it works for the product searches on the front end and in the All Products list in the backend.
But, when I manually add an order and select Add Items -> Add Product the searching in there still uses the product description – is there any way to make this bit ignore the description too?
Thanks
I’m not very familiar with WooCommerce, but I’m guessing that search is not actually powered by Relevanssi in the first place, and likely won’t be possible to handle with Relevanssi.
since wordpress 3.0 there is this funky build in return false function:
add_filter(‘relevanssi_index_content’, ‘__return_false’);
how would i implement this on a post-type basis..? something like: please index post content, but ignore page content..?
add_filter('relevanssi_index_content', '__return_false');
and no, its not that i want to exclude pages from indexing, this is just an example.. i would need a function to remove post content from being indexed, but only for a specific post type..
This is a really blunt instrument, with no adjustment. But you can use “relevanssi_post_content”. It gets two parameters: post content and post object. Check the post object and if conditions match, return an empty string. That does the trick.
thanks! will have a try.
and as mentioned earlier, the __return_false thingy really works 😉
codex
Mikko,
I want to let my shoppers choose which fields they want to include in search. For example:search only titles, or titles & content, or titles & a custom field like year or location. Is there a way to do that? I’m using Relevanssi Pro.
Mike, there’s no easy way to do this right now. I can look at adding this feature to version 2.0.
To filter by custom fields, you can use the custom field parameters.
@Mikko thanks for sharing!
This no longer works as described.
Judy, it should, so please elaborate – what’s the problem, exactly?
Hi Mikko,
If i use this function to not index the content, will it still index excerpts?
I want to ideally disable indexing content, but still titles + custom fields to excerpts function.
Mike, Relevanssi will index excerpts if you make it do so from the settings; it is independent from content indexing.
Hi Mikko,
I have an customer that haven´t updated his site for 2 years. When he updated to the latest version of all plugins plus wordpress, Relevanssi stopped working. I have indexed again and still don´t work. Any ideas? Test searching on the site tpl.se for example ac/dc, metallica or kiss.
Peter, something’s wrong with the theme. If I search for “live in las vegas”, I get this as the search URL with no results:
https://tpl.se/?s=live+in+las+vegas&post_type=product&dgwt_wcas=1
However, if you drop the
dgwt_wcas
part off, you get more results:https://tpl.se/?s=live+in+las+vegas&post_type=product&dgwt_wcas=1
That’s coming from Ajax Search for WooCommerce, so I’d start by disabling that plugin; it may be it’s not compatible with Relevanssi at all. Removing this finds you three results! But drop the post type restriction, and you’ll find 15 pages of results:
https://tpl.se/?s=live+in+las+vegas
Thank you Mikko for your reply.
I have deactivated the Ajax search, and I have indexed again. There is no big difference. I know their is lots of Kiss albums on the site and it´s only showing 2.
Relevanssi returns ten pages of Kiss albums if you lift the post type restriction: https://tpl.se/?s=kiss
If you use the Relevanssi-specific post type parameter, you also get ten pages of results: https://tpl.se/?s=kiss&post_types=product
This would suggest there’s something wrong with the WooCommerce product search results template. I’d start digging there.
Hi Mikko,
Is the code ‘add_filter( ‘relevanssi_index_content’, ‘__return_false’ );’ still valid in WP5, PHP7 and Relevanssi 4.3.3. ?
I used it but no effects. I have 340.000 posts so my re-index took 22 hours.
Joost
PS: I’ve red this page, you are a patient man.
Yes, it’s still valid. Your site has about 300k posts too much for Relevanssi, so I would just recommend not using Relevanssi at all with those kinds of numbers. Even if you block the content, it’s still going to be unpleasant, there’s no way around that.
Hi Mikko,
i am not sure this is right place but i have used this solution on my page and its working. But i am looking for solution that search bar displays product tags when typing not products. Is it even possible?
Thank you for your work.
Relevanssi Premium can return product tags as results instead of products. However whether that works or not depends on the solution you use to display the live results, as that’s not a Relevanssi feature.
Hello!
Can someone help please, i want to search my CPT just by tags but its showing with title also… please have a look on this
https://printedwalls.co.uk/
this is my website link….
i have made tags with the name of dan wilson zain etc
Best Regards
Danish, have you added the code to disable the title indexing and have you rebuilt the index afterwards? If you look at the posts with the Relevanssi debugger (Settings > Relevanssi > Debugging), does the title content appear there?