-
-
Save moskalukigor/42dd1d4deb4206bb1b580e443fccccc5 to your computer and use it in GitHub Desktop.
WP Ajax search (ACF included)
This file contains 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 | |
/* Wordpress Custom Search by title, ACF, post_meta */ | |
// Wordpress ?s= redirect to /search/ | |
function wpa_search_redirect() { | |
global $wp_rewrite; | |
if (!isset($wp_rewrite) || !is_object($wp_rewrite) || !$wp_rewrite->using_permalinks()) { return; } | |
$search_base = $wp_rewrite->search_base; | |
if (is_search() && !is_admin() && strpos($_SERVER['REQUEST_URI'], "/{$search_base}/") === false) { | |
wp_redirect(home_url("/{$search_base}/" . urlencode(get_query_var('s')))); | |
exit(); | |
} | |
} | |
add_action('template_redirect', 'wpa_search_redirect'); | |
// Fix for empty search queries redirecting to home page | |
function wpa_request_filter($query_vars) { | |
if (isset($_GET['s']) && empty($_GET['s']) && !is_admin()) { | |
$query_vars['s'] = ' '; | |
} | |
return $query_vars; | |
} | |
add_filter('request', 'wpa_request_filter'); | |
// Custom AJAX search | |
add_filter( 'posts_search', '__search_by_title_only', 500, 2 ); | |
function __search_by_title_only( $search, &$wp_query ){ | |
global $wpdb; | |
if ( empty( $search ) ) return $search; | |
$q = $wp_query->query_vars; | |
$n = ! empty( $q['exact'] ) ? '' : '%'; | |
$search = $searchand = ''; | |
foreach ( (array) $q['search_terms'] as $term ) { | |
$term = esc_sql( $wpdb->esc_like( $term ) ); | |
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')"; | |
$searchand = ' AND '; | |
} | |
if ( !empty( $search ) ) { | |
$search = " AND ({$search}) "; | |
if ( ! is_user_logged_in() ) | |
$search .= " AND ($wpdb->posts.post_password = '') "; | |
} | |
return $search; | |
} | |
function cf_search_join( $join ) { | |
global $wpdb; | |
$join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id '; | |
return $join; | |
} | |
function cf_search_where( $where ) { | |
global $pagenow, $wpdb; | |
$where = preg_replace( | |
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", | |
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where ); | |
return $where; | |
} | |
function cf_search_distinct( $where ) { | |
global $wpdb; | |
return "DISTINCT"; | |
} | |
function wpa_ajax_search(){ | |
extract($_POST); | |
add_filter( 'posts_join', 'cf_search_join' ); | |
add_filter( 'posts_where', 'cf_search_where' ); | |
add_filter( 'posts_distinct', 'cf_search_distinct' ); | |
$allsearch = new WP_Query("s=".$key."&showposts=-1&post_type=any&post_status=publish"); | |
if($allsearch->have_posts()): while($allsearch->have_posts()) : $allsearch->the_post(); | |
global $post; ?> | |
<p class="cfx"> | |
<a href="<?php the_permalink(); ?>"> | |
<?php if( has_post_thumbnail() ) the_post_thumbnail('thumbnail'); ?> | |
<span><?php the_title(); ?></span> | |
</a> | |
</p> | |
<?php endwhile; else : | |
echo '<mark>There were no search results. <br />Please try using more general terms to get more results.</mark>'; | |
endif; | |
remove_filter( 'posts_join', 'cf_search_join' ); | |
remove_filter( 'posts_where', 'cf_search_where' ); | |
remove_filter( 'posts_distinct', 'cf_search_distinct' ); | |
exit(); | |
} | |
add_action('wp_ajax_wpa_ajax_search', 'wpa_ajax_search'); | |
add_action('wp_ajax_nopriv_wpa_ajax_search', 'wpa_ajax_search'); |
This file contains 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
// WP AJAX Search | |
$.fn.donetyping=function(n,t){t||(t=1e3);var i,u=function(t){i&&(i=null,n(t))};return this.each(function(){var n=$(this);n.on("keyup",function(){i&&clearTimeout(i),i=setTimeout(function(){u(n)},t)}).on("blur",function(){u(n)})}),this}; | |
$('.search_box input[type="text"]').donetyping(function(){ | |
var t = $('.search_box input[type="text"]'), | |
val = t.val(), | |
form = t.parent(); | |
if(val.length >= 3) { | |
$.ajax({ | |
type: 'post', | |
url: $('html').data('a'), | |
data: { | |
action: 'wpa_ajax_search', | |
key: val | |
}, | |
beforeSend: function(){ | |
$('img, .close', form).fadeIn(250); | |
form.next().find('.showsearch').html('<mark>Searching...</mark>'); | |
}, | |
success: function(html) { | |
form.next().find('.showsearch').html(html); | |
$('img', form).fadeOut(250); | |
} | |
}); | |
} else { | |
form.next().find('.showsearch').html('<mark>Enter 3 or more letters</mark>'); | |
$('img', form).fadeOut(250); | |
} | |
}); | |
$(document).on('click', '.search_box i', function(){ | |
$(this).fadeOut(250).prev().fadeOut(250).parent().next().find('>div').html(''); | |
$(this).next().val(''); | |
}); |
This file contains 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
.search_box { | |
@include border-radius(3px); | |
@include transition(); | |
position: relative; | |
z-index: 99; | |
margin: 0; | |
form { | |
input[type="text"] { | |
@include border-radius(5px); | |
padding: 0 30px 0 10px; | |
border: 1px solid #ccc; | |
font: 300 14px $font; | |
background: #fff; | |
display: block; | |
width: 100%; | |
height: 32px; | |
margin: 0; | |
color: #333; | |
&:focus { | |
border-color: #000 | |
} | |
} | |
button { | |
@include border-radius(5px); | |
position: absolute; | |
background: #fff; | |
cursor: pointer; | |
height: 30px; | |
width: 30px; | |
right: 2px; | |
padding: 0; | |
border: 0; | |
top: 1px; | |
&[disabled] { | |
cursor: pointer | |
} | |
&:before { | |
@include border-radius(50%); | |
border: 2px solid #333; | |
@include transition(); | |
position: absolute; | |
height: 13px; | |
width: 13px; | |
content: ""; | |
left: 5px; | |
top: 5px; | |
} | |
&:after { | |
@include transform(rotate(45deg)); | |
@include border-radius(3px); | |
@include transition(); | |
position: absolute; | |
background: #333; | |
content: ""; | |
height: 2px; | |
width: 10px; | |
left: 18px; | |
top: 21px; | |
} | |
&:hover { | |
@include box-shadow(none); | |
&:before {border-color: #16a085} | |
&:after {background: #16a085} | |
} | |
} | |
img { | |
@include border-radius(5px); | |
position: absolute; | |
background: #fff; | |
display: none; | |
padding: 7px; | |
z-index: 99; | |
width: 30px; | |
right: 1px; | |
top: 1px; | |
} | |
.close { | |
font: 400 14px/30px "Verdana",sans-serif; | |
@include border-radius(5px); | |
text-transform: uppercase; | |
@include transition(); | |
text-align: center; | |
position: absolute; | |
background: #fff; | |
cursor: pointer; | |
display: none; | |
height: 30px; | |
width: 30px; | |
z-index: 32; | |
right: 1px; | |
top: 1px; | |
&:hover { | |
color: #ef4036 | |
} | |
} | |
} | |
.search_result { | |
position: absolute !important; | |
font: 400 14px/20px $font; | |
left: 0; | |
max-height: 240px; | |
overflow: auto; | |
right: 0; | |
top: 100%; | |
z-index: -1; | |
.showsearch { | |
padding: 7px 0 0; | |
margin: -7px 0 0; | |
background: #fff; | |
&:empty {display: none} | |
} | |
mark { | |
text-align: center; | |
color: #707070; | |
display: block; | |
padding: 10px; | |
} | |
p { | |
border-top: 1px solid #e1e1e1; | |
text-align: left; | |
padding: 0; | |
&:first-child { border-top: 0 } | |
a { | |
padding: 5px 10px; | |
@include flexbox(); | |
@include justify-content(space-around); | |
@include align-items(center); | |
color: #333; | |
img { | |
width: 40px; | |
display: block; | |
margin: 0 10px 0 0; | |
} | |
span { | |
@include flex(1,0,0); | |
} | |
&:hover { | |
color: #707070 | |
} | |
} | |
} | |
} | |
} |
This file contains 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
<div class="search_box cfx"> | |
<form action="<?php echo domain_name(); ?>" role="search"> | |
<img data-aload="<?php echo theme('images/wpa_preloader.gif'); ?>" src="<?php echo ALOAD; ?>" alt="search" /> | |
<i class="close">x</i> | |
<input type="text" name="s" placeholder="Search..." autocomplete="off" autocapitalize="off" autocorrect="off"> | |
<button type="submit" class="icon-search" disabled></button> | |
</form> | |
<div class="search_result"> | |
<div class="showsearch"></div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment