Continue reading Adding extra weight to AND results in an OR search

Adding extra weight to AND results in an OR search

My Relevanssi engine has the AND operator enabled, with a fallback to OR when the search finds no matches. I’d like the search to display first the AND results and later the OR results. For example, if I search for “wedding party”, I’d like the search to show first the entries matching both words and…

Read more Adding extra weight to AND results in an OR search 0 Comment on Adding extra weight to AND results in an OR search
Continue reading Adding a custom field filter in the search

Adding a custom field filter in the search

Adding a custom field filter in a Relevanssi search takes a little bit of programming, because the filter needs to create a meta_query that Relevanssi understands. Let’s assume we have some colour information in a custom field that’s called colour_field, and we want to use that to filter the search results. First, let’s add a…

Read more Adding a custom field filter in the search 48 Comments on Adding a custom field filter in the search
Continue reading Auto-redirecting with one search result

Auto-redirecting with one search result

If you want to auto-direct the user directly to the result if there’s just one result for the search, that’s quite easy. Add the following code to your site: This is not Relevanssi-specific and works just fine even without Relevanssi. If you are using Relevanssi on a multisite system, you need a small change to…

Read more Auto-redirecting with one search result 15 Comments on Auto-redirecting with one search result
Continue reading Ordering search results by date

Ordering search results by date

I’m building a directory and would like the oldest posts to automatically display first in the search results, is this possible? I found out how to add a link that the user can click to sort the results but would like an automatic solution. Can you provide code that can be added to the functions.php…

Read more Ordering search results by date 3 Comments on Ordering search results by date
Continue reading Using multiple custom taxonomies

Using multiple custom taxonomies

Usually, WordPress supports one category, one tag and one taxonomy in the search, if you use the query variables to set the taxonomies. You can enter multiple taxonomies as parameters to the query, but only the first one is used. If you want to have multiple taxonomies involved, you need to build a tax_query. The…

Read more Using multiple custom taxonomies 52 Comments on Using multiple custom taxonomies
Continue reading Search results in random order

Search results in random order

Do you want to order your search results in random order? Here’s how: Add this function to your site and the search results will appear in random order. You can also do this: However, both of these solutions only work if all the search results are on the same page. If the results span multiple…

Read more Search results in random order 1 Comment on Search results in random order
Continue reading Filtering results by category

Filtering results by category

In order to filter search results by category, use the following code: $url = get_bloginfo(’url’); $url = esc_attr(add_query_arg(array(’cat’ => ’10’, ‘s’ => get_search_query()), $url)); echo "<a href=’$url’ rel=’nofollow’>Filter by category 10</a>";$url = get_bloginfo(‘url’); $url = esc_attr(add_query_arg(array(‘cat’ => ’10’, ‘s’ => get_search_query()), $url)); echo "<a href=’$url’ rel=’nofollow’>Filter by category 10</a>"; Put this in your search results…

Read more Filtering results by category 27 Comments on Filtering results by category
Continue reading Related searches feature

Related searches feature

John Blackbourn wrote a cool related searches feature for Relevanssi Premium. It’s included from version 1.7.4. In order to use the related searches, you must have search logging enabled and a good log of past searches. Then add the following code to your search results template in a suitable place: The first parameter is the…

Read more Related searches feature 21 Comments on Related searches feature
Continue reading Custom weighing with relevanssi_match and relevanssi_results

Custom weighing with relevanssi_match and relevanssi_results

The relevanssi_match hook lets you modify the matches found for queries. It passes a match object, which has the post id ($match->doc), number of hits found in different parts of the post and the weight assigned to the post ($match->weight). Here’s how Relevanssi calculates the weight: Where $idf is the inverse document frequency, aka the…

Read more Custom weighing with relevanssi_match and relevanssi_results 64 Comments on Custom weighing with relevanssi_match and relevanssi_results