Relevanssi Premium 2.4.4 introduced a new feature where you can target specific parts of the post straight from the search terms. The format for this is
{target:search_term}
Target can be one of the following:
- title: Post title.
- content: Post content.
- author: Author display name.
- comment: Comment text.
- link: Link from another post.
- excerpt: Post excerpt.
- taxonomy slug: Taxonomy name (use
tag
orpost_tag
for tags,category
for categories and the taxonomy slug for other taxonomies). - custom field name: Custom field name.
- MySQL column name: Name of a custom MySQL column.
These are all compared against Relevanssi index, so you can use any of these targets only if Relevanssi indexes that particular type of content: if you want to, say, target excerpts, you need to make sure Relevanssi is set to index the excerpt.
The search term must be a single word, so if you want to target multiple words, use a specifier for each word separately. Phrases are not supported at the moment.
Examples:
Search for word “cat” in titles only:
{title:cat}
Search for word “dog”, but only in tags:
{tag:dog}
Search for posts that have the word “knizia” in taxonomy “designers” and the word “dutrait” in the taxonomy “illustrators”:
{designers:knizia} {illustrators:dutrait}
Note that for multiple targeted terms, you need to have a space between the terms.
Hello, mr. Saari! Can you please help to understand where is needed use {target:search_term} exactly? May be it is needed to use hooks like relevanssi_modify_wp_query to modify search terms from backend? Thank you!
It can be used in search terms anywhere where you want to target a specific part of the post with a search. Since it’s a bit clunky to use, I wouldn’t expect your site visitors to use it – though they can – so it’s best used in computer-generated queries. The original reason why I added it was to make Related posts queries more accurate. When you restrict the Related posts queries to only target tags with tag keywords, this is what Relevanssi uses to do that.
So you don’t need any hooks, code or anything like that: just use it in the search terms.
Thank you very much, mr. Saari. This feature is very useful for custom search functionality. Wish you good day!