Skip to main contentSkip to footer

I have a term who name is for, example, Great Blue Trojans. The slug is GBT.

I want to be able to search for ‘GBT’ and return all the posts with this term. When I search ‘Great Blue Trojans’ I get the hits. When I search GBT I do not.

Is this as expected? Is there anything I could do to change if it is?

By default Relevanssi does not index the slug. You can use the relevanssi_content_to_index filter hook to solve this.

Add this to your theme functions.php and rebuild the index, and you should be able to find posts by slug.

add_filter( 'relevanssi_content_to_index', 'rlv_term_slugs', 10, 2 );
function rlv_term_slugs( $content, $post ) {
	$tags = get_the_terms( $post->ID, TAXONOMY );
	if ( false !== $tags ) {
		$tagstr = '';
		foreach ( $tags as $tag ) {
			if ( is_object( $tag ) ) {
				$content .= $tag->slug . ' ';
			}
		}
	}
  	return $content;
}

Replace TAXONOMY with the name of the taxonomy of the terms you want to index – post_tag for tags, category for categories and so on.

Originally asked on WordPress.org.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Indexing with a cron job

…directory. If they’re not, adjust the path to wp-blog-header.php so that it points to the right place. Now to start indexing, have the cron job visit the relevanssi-start-indexing.php – this will start the indexing and will index 100 posts. Then have the cron job visit relevanssi-continue-indexing.php as many times as……this happen in practice, you need two files. First, create a file relevanssi-start-indexing.php with this content: <?php require ‘wp-blog-header.php’; relevanssi_build_index( false, false, 100 ); and then a file relevanssi-continue-indexing.php with this content: <?php require ‘wp-blog-header.php’; relevanssi_build_index( true, false, 100 ); This assumes the files are in your WP installation root…

Search results breakdown by type

Do you want to have a breakdown of search results by post type? This could be used for example to show a list of post types like books, movies, or albums on a review site, and make them links to limit the search results to that post type. This is…

Related Posts:

Comment Section:

7 Comments. Leave new

  • I think the example is missing the final line
    return $content
    ?

    Reply
  • I’ve been trying to search for woocommerce product category slugs. Will this solution work?

    I’m pretty new to wordpress and I’m not a programmer so please forgive my ignorance 🙂
    In this solution what would I put in place of TAXONOMY?

    Thanks, Bob

    Reply
    • See Product > Categories. That page has a list of product categories, and the Slug is listed for each category. Use that slug for the taxonomy name.

      Reply
      • I’m not sure I understand. I have hundreds of different product category slugs that I would like to be indexed for search. Would I have to repeat that function for each slug?
        Sorry to be annoying

        Reply
        • You have hundreds of different categories you want indexed? If so, yes. If you have one category with hundreds of items in it, then you need this just once.

          Reply
          • OK, thank you. I’m trying to make a store to sell products for every team in college football. Each team and conference is a category with a slug. Many of the products can go for multiple teams and therefore into each team category. For example a green and white shirt with a generic logo can go for all the teams that use green and white. All the other green and white products (mugs, hats, etc) go into the team categories as well.

            Thanks for your time. I appreciate it

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed