Last active
June 15, 2018 09:18
-
-
Save ajaydsouza/767215093a24bafb903a to your computer and use it in GitHub Desktop.
Better Search API Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Fixes to disregard Hidden products and to add Author Billing field to Better Search plugin queries | |
function filter_bsearch_posts_join( $join ) { | |
global $wpdb, $author_search; | |
$join .= " | |
INNER JOIN $wpdb->postmeta AS pm ON ($wpdb->posts.ID = pm.post_id) | |
"; | |
if ( $author_search ) { | |
$join .= " | |
INNER JOIN $wpdb->postmeta AS pma ON ($wpdb->posts.ID = pma.post_id) | |
INNER JOIN $wpdb->postmeta AS pmta ON ($wpdb->posts.ID = pmta.post_id) | |
"; | |
} | |
return $join; | |
} | |
add_filter( 'bsearch_posts_join', 'filter_bsearch_posts_join' ); | |
function filter_bsearch_posts_where( $where, $search_term ) { | |
global $wpdb, $author_search; | |
$where .= " | |
AND ( pm.meta_key = '_visibility' AND pm.meta_value = 'visible' ) | |
"; | |
if ( $author_search ) { | |
$where .= " | |
AND ( pma.meta_key = 'wpcf-author-billing' AND pma.meta_value LIKE '%{$search_term}%' ) | |
AND ( pmta.meta_key = 'wpcf-title-alphabetical' ) | |
"; | |
} | |
return $where; | |
} | |
add_filter( 'bsearch_posts_where', 'filter_bsearch_posts_where', 10, 2 ); | |
function filter_bsearch_posts_match( $match, $search_term ) { | |
global $wpdb, $author_search; | |
$author_search = $wpdb->get_results( $wpdb->prepare( " | |
SELECT post_id FROM $wpdb->postmeta | |
WHERE `meta_key` = 'wpcf-author-billing' AND `meta_value` LIKE '%s' | |
", "%".$search_term."%" ) ); | |
if ( $author_search ) { | |
return " "; | |
} else { | |
return $match; | |
} | |
} | |
add_filter( 'bsearch_posts_match', 'filter_bsearch_posts_match', 10, 2 ); | |
function filter_bsearch_posts_orderby( $orderby, $search_term ) { | |
global $wpdb, $author_search; | |
if ( $author_search ) { | |
return " pmta.meta_value ASC "; | |
} else { | |
return $orderby; | |
} | |
} | |
add_filter( 'bsearch_posts_orderby', 'filter_bsearch_posts_orderby', 10, 2 ); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Joins the Post Meta table. Filters bsearch_posts_join. | |
* | |
* @param string $join | |
* @return string | |
*/ | |
function filter_bsearch_posts_join( $join ) { | |
global $wpdb; | |
return $join . " | |
INNER JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) | |
"; | |
} | |
add_filter( 'bsearch_posts_join', 'filter_bsearch_posts_join' ); | |
/** | |
* Checks the '_visibility' meta key for 'visible' meta_value. Filters bsearch_posts_where. | |
* | |
* @param string $join | |
* @return string | |
*/ | |
function filter_bsearch_posts_where( $where ) { | |
global $wpdb; | |
return $where . " | |
AND ( $wpdb->postmeta.meta_key = '_visibility' AND $wpdb->postmeta.meta_value = 'visible' ) | |
"; | |
} | |
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