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

Gutenberg Full Site Editing
There’s a compatibility issue between Relevanssi and Gutenberg Full Site Editing. No results are found when you create a Query…

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