<?php

include('../../../wp-load.php');

if(isset($_GET['term']) && !empty($_GET['term'])):

	$search	=	$_GET['term'];

	$search_arr	=	explode(" ",$search);

	global $wpdb;

	$counter= 0;

	$arr = array();

	$cateid = get_site_option('symatic_strong_cat');

	//OR ".get_like_string($search_arr,'post_title')." 

	$querystr = "

		SELECT $wpdb->posts.ID,$wpdb->posts.post_title,$wpdb->posts.post_content,post_name

		FROM $wpdb->posts JOIN $wpdb->term_relationships 

		ON `bible_term_relationships`.object_id = bible_posts.ID

		WHERE ($wpdb->term_relationships.term_taxonomy_id=4
		
		AND $wpdb->term_relationships.term_taxonomy_id != $cateid)

		AND  (".get_like_string($search_arr,'post_content')."

		OR ".fulltextsearch($search,'post_title').")

		AND $wpdb->posts.post_status = 'publish'

		LIMIT 0,10

		";

	  $result_content = $wpdb->get_results($querystr);

	  $result_count	= count($result_content);

	  if(!empty($result_content)):

		foreach ($result_content as $post):

			$cats = wp_get_post_categories($post->ID);
			if(in_array($cateid, $cats)) continue;

			$post_title	=	get_the_title( $post->ID);
			$post_string=	'('.$post_title.')';

			$arr[$counter]['label'] = strip_tags($post->post_content);
			$arr[$counter]['title'] = $post_string;
			$arr[$counter]['desc'] = $search;

			$url = get_permalink($post->ID)."?v=".$post->post_name;

			$isChapter = (preg_match("/-/",$post->post_name))?true:false;

			if($isChapter){
				$breakout = explode("-", $post->post_name);
				$url = str_replace($post->post_name, $breakout[1], get_permalink($post->ID))."?v=".$post->post_name;
			}

			$arr[$counter]['url'] = $url;
			$arr[$counter++]['count'] = $result_count;

		  endforeach;

	  endif;

	  echo json_encode($arr);

	  exit;

endif;

?>

