I have a pertinent question that I have been trying to solve at times. On my website I have 2 search modes customized and on different pages, however when creating the 3rd I see the results on the page but I can not use the search block. Someone who can give a light ...
if ( !is_admin() ) {
// custom search
add_filter( 'posts_join', 'custom_search_join' );
add_filter( 'posts_where', 'custom_search_where' );
add_filter( 'posts_groupby', 'custom_search_groupby' );
}
// search with join
function custom_search_join($join) {
if ( is_search() && isset($_GET['s'])) {
global $wpdb;
$join = " LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";
}
return($join);
}
// search with groupby
function custom_search_groupby($groupby) {
if ( is_search() && isset($_GET['s'])) {
global $wpdb;
$groupby = " $wpdb->posts.ID ";
}
return($groupby);
}
// search with where
function custom_search_where($where) {
global $wpdb;
$old_where = $where;
if (is_search() && isset($_GET['s']) && !isset($_GET['segundo_search']))
{
// add additional custom fields here to include them in search results
$customs = array('_mais1', 'mais2', '_mais3', 'mais4');
$query = '';
$var_q = stripslashes($_GET['s']);
preg_match_all('/".*?("|$)|((?<=[\s",+])|^)[^\s",+]+/', $var_q, $matches);
$search_terms = array_map(create_function('$a', 'return trim($a, "\"\'\n\r ");'), $matches[0]);
$n = '%';
$searchand = '';
foreach((array)$search_terms as $term) {
$term = addslashes_gpc($term);
$query .= "{$searchand}(";
$query .= "($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$query .= " OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
foreach($customs as $custom) {
$query .= " OR (";
$query .= "($wpdb->postmeta.meta_key = '$custom')";
$query .= " AND ($wpdb->postmeta.meta_value LIKE '{$n}{$term}{$n}')";
$query .= ")";
}
$query .= ")";
$searchand = ' AND ';
}
$term = $wpdb->escape($var_q);
$where .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$where .= " OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
if (!empty($query)) {
$where = " AND ({$query}) AND ($wpdb->posts.post_status = 'publish') AND ($wpdb->posts.post_type = 'primeiro_listing')";
}
} else if (is_search() && isset($_GET['s'])) {
// added custom fields here to include them in search results
$customs = array(
'_1',
'_2',
'_3'
);
$query = '';
$searchand = '';
foreach((array)$search_terms as $term) {
$term = addslashes_gpc($term);
$query .= "{$searchand}(";
$query .= "($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$query .= " OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
foreach($customs as $custom) {
$query .= " OR (";
$query .= "($wpdb->postmeta.meta_key = '$custom')";
$query .= " AND ($wpdb->postmeta.meta_value LIKE '{$n}{$term}{$n}')";
$query .= ")";
}
$query .= ")";
$searchand = ' AND ';
}
$term = $wpdb->escape($var_q);
$where .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$where .= " OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
if (!empty($query)) {
$where = " {$old_where} AND ({$query}) AND ($wpdb->posts.post_status = 'publish') AND ($wpdb->posts.post_type = 'segundo') ";
}
}
return($where);
}
Form:
<?php global $app_abbr, $header_search; $header_search = true; ?>
<?php if (get_option('jr_show_searchbar')!=='no' && ( !isset($_GET['submit']) || ( isset($_GET['submit']) && $_GET['submit']!=='true' ) ) && ( !isset($_GET['myjobs']) || ( isset($_GET['myjobs']) && $_GET['myjobs']!=='true' ) ) ) : ?>
<form action="<?php echo esc_url( home_url() ); ?>/" method="get" id="searchform">
<div class="search-wrap">
<div>
<input type="hidden" name="terceiro_search" value="true" />
<input type="text" id="search" title="" name="s" class="text" placeholder="<?php _e('Search Terceiros',APDB); ?>" value="<?php if (isset($_GET['s'])) echo esc_attr(get_search_query()); ?>" />
<input type="text" id="near" title="<?php _e('Location',APDB); ?>" name="location" class="text" placeholder="<?php _e('Location',APDB); ?>" value="<?php if (isset($_GET['location'])) echo esc_attr($_GET['location']); ?>" />
<label for="search"><button type="submit" title="<?php _e('Go',APDB); ?>" class="submit"><?php _e('Go',APDB); ?></button></label>
<input type="hidden" name="ptype" value="<?php echo esc_attr( POST_TYPE_TERCEIRO ); ?>" />
<input type="hidden" name="latitude" id="field_latitude" value="" />
<input type="hidden" name="longitude" id="field_longitude" value="" />
<input type="hidden" name="full_address" id="field_full_address" value="" />
<input type="hidden" name="north_east_lng" id="field_north_east_lng" value="" />
<input type="hidden" name="south_west_lng" id="field_south_west_lng" value="" />
<input type="hidden" name="north_east_lat" id="field_north_east_lat" value="" />
<input type="hidden" name="south_west_lat" id="field_south_west_lat" value="" />
</div>
</div><!-- end search-wrap -->
</form>
<?php endif; ?>