Skip to main contentSkip to footer

If Polylang is in use and you haven’t enabled “Media translation“, your attachment files won’t have a language. That’s fine, except when Relevanssi searches fail to find your attachment because they’re looking for posts in a specific language.

Suppose you want to include attachments in Polylang searches. In that case, you can either enable “Media translate” so that your media files have a language, or you can use this code in your theme functions.php to always include attachments in the results:

add_filter(
	'relevanssi_where',
	function( $query ) {
	  global $wpdb;
	  
	  $query  = str_replace( 'AND relevanssi.doc', 'AND ( relevanssi.doc', $query );
	  $query .= ' OR relevanssi.doc IN (
	  		SELECT DISTINCT(ID) FROM ' . $wpdb->posts . ' WHERE post_type = "attachment"
		) OR relevanssi.doc = -1 )';
  
	  return $query;
	}
);

This function hooks onto the relevanssi_where filter hook to modify the Polylang taxonomy query so that the search always includes the attachments. This only works if the query uses no other query variables except the Polylang language query. The results may be unexpected if there are other query filters applied.

Your account

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

Search

Popular Resources

WPML: Indexing synced posts
When a post is updated, WPML updates all the translations of the post, but Relevanssi only indexes the post that…
EmbedPress autoembeds
EmbedPress has an auto-embed feature that tries to embed all URLs found in posts. This can cause problems with Relevanssi:…
Automatic redirects to category archives

While Relevanssi Premium can return taxonomy terms as search results, sometimes sending the searcher directly to the category archive is better. This is especially true for WooCommerce sites, where displaying taxonomy terms in search results pages is complicated. Relevanssi Premium has a redirect feature you can use to redirect searches…

Related Posts:

Comment Section:

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