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

PostX Pro
If you use PostX Pro to build a search results template using their Post Grid block, you’ll notice that the…
Modal search form shortcode

…search restrictions as the regular searchform shortcode. Parameters that are not modal settings are passed to the search form. Search Search this site Search for: will restrict the search form to just products. Search Search this site Search for: will restrict the search form to posts, pages and news. Search…Relevanssi includes a modal search form shortcode that opens the search form in a dialog. The shortcode is relevanssi_modal_search. Search Search this site Search for: will display a “Search” button that opens the modal search form. Changing the labels Use the label parameter to change the text on the button:……Search this site Search for: will restrict the search form to categories 10, 14 and 17. Search Search this site Search for: will do a multisite search on blogs 1, 2 and 3. These parameters are added to the search form as hidden input fields. Adding dropdowns The modal shortcode…

EmbedPress autoembeds
EmbedPress has an auto-embed feature that tries to embed all URLs found in posts. This can cause problems with Relevanssi:…

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