WordPress Default Search is Not cool. we are going to create WordPress Ajax Search without plugin that will look something like this.
First We need to Create an input Field so users can search. Put it anywhere page template, single.php, archive.php, index.php, page.php, or anywhere you want to show the search field
<div class="search_bar">
<form action="/" method="get" autocomplete="off">
<input type="text" name="s" placeholder="Search Code..." id="keyword" class="input_search" onkeyup="mukto_search_fetch()">
<button>
Search
</button>
</form>
<div class="search_result" id="datafetch">
<ul>
<li>Please wait..</li>
</ul>
</div>
</div>
Main code for WordPress Ajax Search without plugin
Now put the below code to functions.php On the post query you can customize your HTML as you want. This code will interact with HTML to achieve our goal of creating wp Ajax Search without plugin.
<?php
/*
==================
Ajax Search
======================
*/
// add the ajax fetch js
add_action( 'wp_footer', 'ajax_fetch' );
function ajax_fetch() {
?>
<script type="text/javascript">
function mukto_search_fetch(){
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
success: function(data) {
jQuery('#datafetch').html( data );
}
});
}
</script>
<?php
}
// the ajax function
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
$the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => array('page','post') ) );
if( $the_query->have_posts() ) :
echo '<ul>';
while( $the_query->have_posts() ): $the_query->the_post(); ?>
<li><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></li>
<?php endwhile;
echo '</ul>';
wp_reset_postdata();
endif;
die();
}
Ajax Search for Custom post type
This code will be show result from page & post but if you want you can activate if for your wp custom post type as well. Simply you need to change ‘post_type’ => array(‘page’,’post’) to ‘post_type’ => array(‘your_custom_post_type’)
If search not work use this code [Most of the time not need ]
/**
* This function modifies the main WordPress query to include an array of
* post types instead of the default 'post' post type.
*
* @param object $query The main WordPress query.
*/
function mukto_post_type_include( $query ) {
if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
$query->set( 'post_type', array( 'post', 'page', 'custom_post_type' ) );
}
}
add_action( 'pre_get_posts', 'mukto_post_type_include' );
Learn more about WordPress Ajax here
WP ajax search result issue [ Fixed ]
Our WordPress Ajax Search without plugin Is working 😍 But there is still an issue. Sometimes it shows all posts when you do not write anything on search. To fix this let’s write some jQuery so search results only show when you have more than 2 characters on the search filed
$("input#keyword").keyup(function() {
if ($(this).val().length > 2) {
$("#datafetch").show();
} else {
$("#datafetch").hide();
}
});
div.search_result {
display: none;
}
Put this code on any js file linked to your theme or child theme. That’s all.
To show password-protected posts in search in the search result
Put this code on functions.php
add_filter( 'posts_search', 'include_password_posts_in_search' );
function include_password_posts_in_search( $search ) {
global $wpdb;
if( !is_user_logged_in() ) {
$pattern = " AND ({$wpdb->prefix}posts.post_password = '')";
$search = str_replace( $pattern, '', $search );
}
return $search;
}
Check Out WooCommerce Ajax Search without Plugin Code
WooCommerce Category base Ajax Product Search Without Plugin
Thank you!