Skip to content

Instantly share code, notes, and snippets.

@ajaydsouza
Last active April 25, 2020 18:18
Show Gist options
  • Save ajaydsouza/5f84bf59dbf6a460a2708a37d1a407ae to your computer and use it in GitHub Desktop.
Save ajaydsouza/5f84bf59dbf6a460a2708a37d1a407ae to your computer and use it in GitHub Desktop.
Better Search and Polylang compatibility
<?php
// Doesn't work.
/**
* Joins the taxonomy tables. Filters bsearch_posts_join.
*
* @param string $join
* @return string
*/
function filter_bsearch_posts_join( $join ) {
global $wpdb;
$join .= "
INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.post_id = {$wpdb->term_relationships}.object_id)
INNER JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id)
";
return $join;
}
add_filter( 'bsearch_posts_join', 'filter_bsearch_posts_join' );
/**
* Select posts from the same language. Filters bsearch_posts_where.
*
* @param string $where
* @return string
*/
function filter_bsearch_posts_where( $where ) {
global $wpdb;
$where .= "
AND {$wpdb->term_taxonomy}.taxonomy = 'language'
AND {$wpdb->term_taxonomy}.term_id IN ( SELECT TT.term_id FROM {$wpdb->term_taxonomy} TT INNER JOIN {$wpdb->term_relationships} TR ON TR.term_taxonomy_id = TT.term_taxonomy_id WHERE TR.object_id = {$wpdb->ID} )
";
return $where;
}
add_filter( 'bsearch_posts_where', 'filter_bsearch_posts_where' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment