If the filtering capabilities that Relevanssi provides are not enough, you can take the manual approach and filter the results by whatever you want.
To do that, you need to use the relevanssi_hits_filter filter hook. Here’s how you filter by custom field. Let’s assume you have a custom field called “colour” and want to use a query variable colour to filter results like this: http://www.example.com/?s=ford&colour=red.
First you have to introduce your query variable to WordPress, otherwise WP will just clean it out. Add all the code to your theme’s functions.php.
Would you be able to add this to the search form via a text input field? EG my client has publications set up as posts and wants to be able to search by publication date. I have added a custom field called pub_date and was hoping to use this plugin to allow the users to input what date (ie year) they want to search (along with the category/taxonomy).
P.S. I’m using the premium plugin.
Yes, the code doesn’t care about the input field – in the example “colour” can be text input field, dropdown, checkbox or whatever you wish. The value is only thing that matters for this code.
The code should pretty much work as it is, if you just change every “colour” to “pub_date”.
okay so if I add the above code into my functions.php file, replace every colour with pub_date, then I should be able to add a text input field to the search form that will filter the results by publication date?
It includes two custom taxonomies already. I’m just a bit lost as to how to add code to the form to include the custom field input area. If you had any ideas that would be great!
The comment form eats up HTML code, so please send me the code to mikko @ mikkosaari.fi.
This filter doesn’t add the search terms to the Relevanssi index, does it? And if not, is there a way to build the index from certain custom meta values?
After browsing Relevanssi code, I found the “Custom fields to index” setting in the administration settings page. I had not seen this previously, and appears that it will accomplish what I need.
If colour and shape are taxonomies, that should work automatically just like that.
colour and shape are custom fields..
Ok, in that case just follow the example above and just add another field.
If you want me to do this for you, I’m available for hire.
My custom field name is “categoria_profissional”. It is a checkbox field. I want to display several checkboxes with the values of this field on a search page. The user will select one of the “categoria_profissional” value and will click on search or will add some text on the search field.
But it didnt work, maybe if you could just read to see if i did something wrong with the code, it would be great!
How it didn’t work? Have you debugged the code at all, to see where it goes wrong? Is the $wp_query->query_vars[‘categoria_profissional’] set? If it is, what’s in it? Does the get_user_meta() fetch the correct data?
Go through the functions and see where it doesn’t work like it should.
I don’t know how the Xili language plugin works, but take a look at lib/common.php for the WPML and Polylang compatibility function – that’s probably what you need to do with Xili.
Thanks Mikko for your response, basicaly we can add a paramater to wp query which is lang to query elements by language. I don’t know if there is any possibility to do the same with your powerfull plugin.
I’ve added a parameter to the search from but i don’t think that it is taken in consideration.
Relevanssi can be made compatible with Xili, but as I don’t know how Xili works, I don’t know exactly how. Relevanssi needs to be told how to interpret that “lang” parameter, or you can use it to filter the results afterwards, like is done with WPML and Polylang.
i hope it can help someone. i’m not sure if it is well coded.
Sounds like a bunch of stopwords to me, so you can try removing the words from the list of stopwords and rebuilding the index.
Hi Mikko, I’ve used your plugin many times already, but I have another question for a new project.
Is it possible to index using post title and content, but then choose which to use when searching on the front end? For example, can I enter a search term, and then select a radio button of ‘Search by title only’ or ‘Search all content’?
Yes, but it’s somewhat complicated. You can use the relevanssi_match filter to recalculate the weights, and there you will know whether the hit was found in titles or in content, and you can use that to zero the weight if the hit was in the wrong place.
Hi Mikko and Keith, have you got further with this issue, or a code example of how that could be done? I need a simple search box that searches for strings in my custom post titles, and that seems to be quite a problem, any help appreciated.
No, nothing ready. This is a fairly simple programming task, but without programming it can be quite tricky to achieve, indeed.