Skip to content

Instantly share code, notes, and snippets.

@insaurabh
Last active September 25, 2018 12:24
Show Gist options
  • Save insaurabh/59ce5ee1fab8b7fd50e3531a2c9f357d to your computer and use it in GitHub Desktop.
Save insaurabh/59ce5ee1fab8b7fd50e3531a2c9f357d to your computer and use it in GitHub Desktop.
ajax search for title and custom meta in wordpress
/**
* Shortcode for property serach form
* @author Saurabh Ranjan
* <button type="button">Review Property</button>
*/
TODO:
1) chagne `post_type` as per your need
2) change `custom_meta` as per your need
3) Update $HTML as per need
4) change variable as per need
What this code supposed to do
1) Get all list of ids metaching for meta
2) Gel all list of ids metchangin for title
3) Merge all the above ids list
4) Remove duplicate ids
5) Now we have list of ids matching our search ( do what you want to do with them )
add_shortcode('search_property_form', 'search_property_form');
function search_property_form()
{
// $loaderImg = hb_upload_dir_var( 'baseurl', '/cutomimages', '/loader.gif' );
// $formHtml = "<img src=".$loaderImg." class='hb-loader' style='display: none;' title='please wait' alt='please wait'>";
$formHtml .= '<div class="head-search search-property-page"><div class="search-property-btn"></div><form role="search" method="get" class="searchPropertyForm" action="' . home_url('/') . '" >
<div class="search-form-product"><div class="search-bar">
<input type="text" name="property-search" class="searchPropertyInput" placeholder="Property Search" autocomplete="off" />
<span style="display:none" class="error-min-len-msg">Min 4 character needed </span>
</div>
<div class="property-suggest-ajax"></div>
</form></div>
</div>';
$formHtml .= '';
return $formHtml;
}
// ajax function
add_action('wp_footer', 'some_prefix_property_search');
add_action('wp_footer', 'some_prefix_add_loader');
if(!function_exists('some_prefix_add_loader')) {
function some_prefix_add_loader() {
$loaderHTML = "<div class='hb-page-loader'><div class='loader'>Loading...</div></div>";
echo $loaderHTML;
}
}
/* Property search form | Get data and process | Ajax */
if( !function_exists('some_prefix_property_search')){
function some_prefix_property_search() {
$admin_ajax_url_prop_search = admin_url('admin-ajax.php')."?action=some_prefix_property_ajax";
?>
<script type="text/javascript">
jQuery(function($){
$('.searchPropertyInput').on('keyup', function() {
var value = this.value.trim();
if( value.length < 4){
this.classList.add('error-min-len');
$('.error-min-len-msg').show();
$('.property-suggest-ajax').text('');
return;
} else {
$('.error-min-len-msg').hide();
this.classList.remove('error-min-len');
$.ajax({
type:'post',
datatype:'json',
data:{search_text:value},
url:"<?php echo $admin_ajax_url_prop_search?>",
success:function(response){
$('.property-suggest-ajax').html(response);
}
});
}
});
});
</script>
<?php }
}
// action to get post id list based on title and one custom meta you can change as per your need
function some_prefix_property_ajax()
{
global $post;
$HTML = '';
$search_string = esc_attr(trim($_REQUEST['search_text']));
/* get list of post id based on meta suggest */
$idsBasedOnMeta = array(
'posts_per_page' => -1,
'post_type' => 'property',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'mkdf_property_full_address_meta',
'value' => "{$search_string}",
'compare' => 'LIKE'
),
),
);
$idsBasedOnMeta = new WP_Query( $idsBasedOnMeta );
if ( $idsBasedOnMeta->have_posts() ) {
// get all ids matching meta
$post_ids_based_meta = wp_list_pluck( $idsBasedOnMeta->posts, 'ID' );
/* Restore original Post Data */
wp_reset_postdata();
}
$idsBasedOnPostTitle = array(
'posts_per_page' => -1,
'post_type' => 'property',
'post_status' => 'publish',
's' => "{$search_string}"
);
$idsBasedOnPostTitle = new WP_Query( $idsBasedOnPostTitle );
if ( $idsBasedOnPostTitle->have_posts() ) {
// get all ids matching meta
$post_ids_based_title = wp_list_pluck( $idsBasedOnPostTitle->posts, 'ID' );
/* Restore original Post Data */
wp_reset_postdata();
}
$final_post_ids_list = array_unique(array_merge((array)$post_ids_based_meta, (array)$post_ids_based_title),SORT_REGULAR);
if($final_post_ids_list){
$args = array(
// 'posts_per_page' => 10,
'post_type' => 'property',
'post_status' => 'publish',
'post__in' =>$final_post_ids_list
);
$postslist = get_posts( $args );
$HTML = '<ul class="property-list-item">';
foreach ( $postslist as $post ) {
$HTML .= '<li><span class="fa fa-map-marker"></span>';
$HTML .= "<a href='". get_permalink() ."'>".$post->post_title."</a>";
$HTML .= '</li>';
// echo "<br/>";
}
$HTML .= '</ul>';
wp_reset_postdata();
if(!is_user_logged_in()) {
$HTML .= '<p class="redirect-sign">';
$HTML .= "<span class='redirectToReview'>SIGN IN or SIGN UP</span> to review these property!";
$HTML .= '</p>';
}
} else {
$HTML .= "<div class='not-found-result'>We do not have <strong>".$search_string." </strong>in our database.</div>";
if(!is_user_logged_in()) {
$HTML .= '<p class="redirectToReview">';
$HTML .= "Please <span>SIGN IN</span> or <span>SIGN UP</span> to be the first to review this property!";
$HTML .= '</p>';
}
}
$HTML .= '<p class="sdgrf">';
$HTML .= '<div class="redirect-button">';
$HTML .= '<span class="redirectToReview">Suggest a property. It will take just 2 minutes</span>';
$HTML .= '</div>';
$HTML .= '</p>';
_e($HTML);
// end
die();
}
add_action('wp_ajax_some_prefix_property_ajax', 'some_prefix_property_ajax');
add_action('wp_ajax_nopriv_some_prefix_property_ajax', 'some_prefix_property_ajax');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment