<?php
if(strstr($_SERVER['REQUEST_URI'],'/wp-admin/'))
{
	remove_filter('posts_where', 'search_cal_event_where');
	remove_filter('posts_orderby', 'searching_filter_orderby');
	remove_filter('posts_join', 'searching_filter_join');
	remove_filter('posts_where', 'searching_filter_where');
	remove_filter('posts_orderby', 'review_highest_orderby');
	remove_filter('posts_orderby', 'ratings_most_orderby');
	remove_filter('posts_orderby', 'archive_filter_orderby');
}



add_action('pre_get_posts', 'search_filter');
function search_filter($local_wp_query) {
	if(is_search() && $_REQUEST['s']=='Calender-Event')
	{
		add_filter('posts_where', 'search_cal_event_where');		
	}elseif(is_search() && $_REQUEST['t']==CUSTOM_POST_TYPE1)
	{
		add_filter('posts_orderby', 'searching_filter_orderby');
		add_filter('posts_where', 'searching_filter_where');
	}elseif(is_author())
	{
		add_filter('posts_where', 'author_filter_where');	
	}
}
function search_cal_event_where($where)
{
	global $wpdb,$wp_query;
	$m = $wp_query->query_vars['m'];
	$py = substr($m,0,4);
	$pm = substr($m,4,2);
	$pd = substr($m,6,2);
	$the_req_date = "$py-$pm-$pd";
	$event_of_month_sql = "select p.ID from $wpdb->posts p where p.post_type='".CUSTOM_POST_TYPE1."' and p.ID in (select pm.post_id from $wpdb->postmeta pm where pm.meta_key like 'st_date' and pm.meta_value <= \"$the_req_date\" and pm.post_id in ((select pm.post_id from $wpdb->postmeta pm where pm.meta_key like 'end_date' and pm.meta_value>=\"$the_req_date\")))";
	$where = " AND $wpdb->posts.post_type='".CUSTOM_POST_TYPE1."' AND $wpdb->posts.ID in ($event_of_month_sql) and $wpdb->posts.post_status in ('publish','private')";
	return $where;
}
function searching_filter_orderby($orderby) {
	global $wpdb;
	$orderby = "  (select $wpdb->postmeta.meta_value from $wpdb->postmeta where $wpdb->postmeta.post_id=$wpdb->posts.ID and $wpdb->postmeta.meta_key like \"is_featured\") desc,$wpdb->posts.post_title ";
	return $orderby;	
}
function author_filter_where($where)
{
	global $wpdb,$current_user,$curauth,$wp_query;
	$query_var = $wp_query->query_vars;
	$user_id = $query_var['author'];
	$where = " AND ($wpdb->posts.post_author = $user_id) ";
	if($current_user->data->ID==$user_id)
	{
		$where .= " AND ($wpdb->posts.post_type = 'post' OR $wpdb->posts.post_type = '".CUSTOM_POST_TYPE1."') AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private' OR $wpdb->posts.post_status = 'draft') ";
	}else
	{
		$where .= " AND ($wpdb->posts.post_type = '".CUSTOM_POST_TYPE1."')AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private')  ";
	}
	return $where;
}
function searching_filter_join($join) {
	global $wpdb;
	$saddress = trim($_REQUEST['saddress']);
	$sdate = trim($_REQUEST['sdate']);
	if($sdate || $saddress)
	{
		$join .= " join $wpdb->postmeta on $wpdb->postmeta.post_id=$wpdb->posts.ID ";
	}
	return $join;
}

function searching_filter_where($where) {
	global $wpdb;
	$skw = trim($_REQUEST['skw']);
	$scat = trim($_REQUEST['scat']);
	$saddress = trim($_REQUEST['saddress']);
	$sdate = trim($_REQUEST['sdate']);
	
	if($saddress )
	{
		add_filter('posts_join', 'searching_filter_join');
	}
	
	$where = '';
	$where = " AND $wpdb->posts.post_type in ('".CUSTOM_POST_TYPE1."') AND ($wpdb->posts.post_status = 'publish') ";
	if($skw)
	{
		$where .= " AND (($wpdb->posts.post_title LIKE \"%$skw%\") OR ($wpdb->posts.post_content LIKE \"%$skw%\")) ";
	}
	if($sdate)
	{
		$where .= " AND $wpdb->posts.ID in (select pm.post_id from $wpdb->postmeta pm where pm.meta_key like 'st_date' and pm.meta_value <= \"$sdate\" and pm.post_id in ((select pm2.post_id from $wpdb->postmeta pm2 where pm2.meta_key like 'end_date' and pm2.meta_value>=\"$sdate\"))) ";
	}
	if($scat>0)
	{
		$where .= " AND  $wpdb->posts.ID in (select $wpdb->term_relationships.object_id from $wpdb->term_relationships join $wpdb->term_taxonomy on $wpdb->term_taxonomy.term_taxonomy_id=$wpdb->term_relationships.term_taxonomy_id and $wpdb->term_taxonomy.term_id=\"$scat\" ) ";
	}
	if($saddress)
	{
		$where .= " AND ($wpdb->postmeta.meta_key like 'address' and $wpdb->postmeta.meta_value like \"%$saddress%\") ";
	}
	return $where;
}
function searching_no_filter_where($where) {
	global $wpdb;
	$s = trim($_REQUEST['s']);
	$where = " AND $wpdb->posts.post_type  in ('post','".CUSTOM_POST_TYPE1."') AND (($wpdb->posts.post_title LIKE \"%$s%\") OR ($wpdb->posts.post_content LIKE \"%$s%\") OR ($wpdb->postmeta.meta_key like 'address' and $wpdb->postmeta.meta_value like \"%$s%\"))) ";
	return $where;
}

?>