Last active
May 4, 2022 03:22
-
-
Save 1naveengiri/e6534c13793985019581015a21e00e11 to your computer and use it in GitHub Desktop.
Search by tags Geodirectory
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 | |
add_filter( 'geodir_search_terms_where', 'geodir_search_terms_where_custom_callback'); | |
function geodir_search_terms_where_custom_callback( $terms_where ){ | |
if ( geodir_is_page( 'search' ) ) { | |
global $wpdb, $geodir_post_type, $plugin_prefix, $dist, $snear, $s, $s_A, $s_SA, $search_term, $geodirectory; | |
$searc_term_names = explode( ' ', $s ); | |
$table = geodir_db_cpt_table( $geodir_post_type ); | |
$s = trim( $s ); | |
$s = wp_specialchars_decode( $s, ENT_QUOTES ); | |
$s_A = wp_specialchars_decode( $s_A, ENT_QUOTES ); | |
$terms_sql = ''; | |
if ( isset( $_REQUEST['stype'] ) ) { | |
$post_types = esc_attr( wp_strip_all_tags( $_REQUEST['stype'] ) ); | |
} else { | |
$post_types = 'gd_place'; | |
} | |
/* get taxonomy */ | |
$taxonomies = geodir_get_taxonomies( $post_types, true ); | |
if ( $taxonomies ) { | |
$taxonomies = implode( "','", $taxonomies ); | |
$taxonomies = "'" . $taxonomies . "'"; | |
} else { | |
$taxonomies = ''; | |
} | |
$gd_tagmatch_part = ''; | |
// Exact search with quotes | |
$gd_exact_search = false; | |
if ( $s != '' ) { | |
$search_keyword = trim( wp_specialchars_decode( stripslashes( $s ), ENT_QUOTES ), '"' ); | |
$match_keyword = wp_specialchars_decode( stripslashes( $s ), ENT_QUOTES ); | |
if ( strpos( $match_keyword, '"' ) !== false && ( '"' . $search_keyword . '"' == $match_keyword ) ) { | |
$gd_exact_search = true; | |
} | |
// remove quotes after checking if its an exact search, this is VERY IMPORTANT | |
$s = $search_keyword; | |
} | |
$terms_new_where = ''; | |
if( !empty( $searc_term_names )){ | |
foreach( $searc_term_names as $key => $searc_term_name ){ | |
$terms_new_where .= $wpdb->prepare( " OR $wpdb->terms.name LIKE %s", array( $searc_term_name . '%' ) ); | |
} | |
} | |
if ( $gd_exact_search ) { | |
$terms_where = $wpdb->prepare( " AND ($wpdb->terms.name LIKE %s ) ", array( $s ) ); | |
} else { | |
$terms_where = $wpdb->prepare( " AND ($wpdb->terms.name LIKE %s OR $wpdb->terms.name LIKE %s OR $wpdb->terms.name IN ($s_A) ". $terms_new_where ." ) ", array( $s . '%', '%'. $s . '%' ) ); | |
} | |
} | |
return $terms_where; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment