Quantcast
Channel: polylang – WordPress.org Forums
Viewing all articles
Browse latest Browse all 11471

matijsnijland on "Search results should show only Dutch or English posts (listings)"

$
0
0

Hi Chouby,

There is no 500 error. However, the search module still does not show language specific results. I have a feeling that the if statement is not passed. I hope you have an answer. Thanx for the helps so-far. Best regards, Matijs

function cps_search_posts(){

		global $CPS_OPTIONS;
		global $wpdb;

		$join  = '';
		$where = '';
		$order = '';
		$joinedMeta = array();
		$i = 0;
		// Custom fields
		foreach($CPS_OPTIONS['meta_boxes_vars'] as $meta_boxes){
			foreach($meta_boxes as $metaBox){
				$mb_name = $metaBox['name'];
				if(isset($_GET[$mb_name]) && trim($_GET[$mb_name]) != ''){

					$join .= " JOIN $wpdb->postmeta meta$i
											ON meta$i.post_id = p.ID
											AND meta$i.meta_key = '_$mb_name' ";

					if($metaBox['type'] === 'range'){
						$pieces = explode('-',$_GET[$mb_name]);
						$where .= " AND meta$i.meta_value BETWEEN $pieces[0]+0 AND $pieces[1]+0 ";
					} else {
						$where .= " AND meta$i.meta_value = '{$_GET[$mb_name]}' ";
					}

					$joinedMeta["meta$i"] = $mb_name;
					// Order:

					if(isset($_GET["order"])){
						if($_GET["order"] === $mb_name){
							$asc = isset($_GET["orderdirection"]) ? $_GET["orderdirection"] : 'ASC';
							$order .= " meta$i.meta_value $asc ";
						}
					}

	//				$j = 1;
	//				while(isset($_GET["order$j"])){
	//					if($_GET["order$j"] === $mb_name){
	//						$asc = isset($_GET["orderdirection$j"]) ? $_GET["orderdirection$j"] : 'ASC';
	//						$order .= " meta$i.meta_value $asc,";
	//					}
	//					$j++;
	//				}
					$i++;
				} else {
					if(isset($_GET["order"])){
						if($_GET["order"] === $mb_name){
							$asc = isset($_GET["orderdirection"]) ? $_GET["orderdirection"] : 'ASC';
							$join .= " LEFT JOIN $wpdb->postmeta meta$i
													ON meta$i.post_id = p.ID
													AND meta$i.meta_key = '_$mb_name' ";
							$order .= " meta$i.meta_value $asc ";
							$i++;
						}
					}
				}

	//			else {
	//				$j = 1;
	//				while(isset($_GET["order$j"])){
	//					if($_GET["order$j"] === $mb_name){
	//						$asc = isset($_GET["orderdirection$j"]) ? $_GET["orderdirection$j"] : 'ASC';
	//						$join .= " JOIN $wpdb->postmeta meta$i
	//												ON meta$i.post_id = p.ID
	//												AND meta$i.meta_key = '_$mb_name' ";
	//						$order .= " meta$i.meta_value $asc,";
	//						$i++;
	//					}
	//					$j++;
	//				}
	//			}
			}
		}
			// Custom Taxonomies
			$is_search_by_tax = false;
			if( isset($CPS_OPTIONS['taxonomies']) && !empty($CPS_OPTIONS['taxonomies']) ){
				foreach($CPS_OPTIONS['taxonomies'] as $taxonomy){
					if(isset($_GET[$taxonomy]) && trim($_GET[$taxonomy] != '')){
		    $sAlias = preg_replace('#\W#', '_', $_GET[$taxonomy]);
						$is_search_by_tax = true;
						$where .= " AND terms_" .$sAlias . ".name = '{$_GET[$taxonomy]}' ";

		    $join .= "
		     JOIN $wpdb->term_relationships tr_" .$sAlias . " ON tr_" .$sAlias . ".object_id = p.ID
		     JOIN $wpdb->term_taxonomy tt_" .$sAlias . " ON tr_" .$sAlias . ".term_taxonomy_id = tt_" .$sAlias . ".term_taxonomy_id
		     JOIN $wpdb->terms terms_" .$sAlias . " ON terms_" .$sAlias . ".term_id = tt_" .$sAlias . ".term_id
		  ";
					}
				}
			}
	/*
			if($is_search_by_tax){
				$join .= "
				 JOIN $wpdb->term_relationships tr ON tr.object_id = p.ID
				 JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
				 JOIN $wpdb->terms terms ON terms.term_id = tt.term_id
			";
			}
	*/
			// Pagination:
			$page_num = isset($_GET['page']) ? (int)$_GET['page'] : 1;
			$from = $CPS_OPTIONS['per_page']*($page_num-1);
			$count = $CPS_OPTIONS['per_page'];

			// Order:

			$order_by = '';
			if(!empty($order)){
				$order_by = "ORDER BY ".rtrim($order, ',');
			}

			$in_posts = implode("','", $CPS_OPTIONS['post_types']);

			/*global $polylang;

			if (isset($polylang) && class_exists('Polylang_Core') && $polylang instanceof Polylang_Core && isset($polylang->curlang))
			{
			$join .= "INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id =";
			$where .= $wpdb->prepare("AND pll_tr.term_taxonomy_id IN(%s)");
			$polylang->curlang->term_taxonomy_id);
			}*/
			global $polylang;
if (isset($polylang) && class_exists('Polylang_Core') && $polylang instanceof Polylang_Core && isset($polylang->curlang))
{
$join.= "INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = p.ID";
$where.= $wpdb->prepare("AND pll_tr.term_taxonomy_id = %d",$polylang->curlang->term_taxonomy_id);
}

			 $query = "
            SELECT
               p.*
            FROM {$wpdb->base_prefix}posts p
               $join
            WHERE p.post_status = 'publish'
               -- Only custom posts:
               AND p.post_type IN ('$in_posts')
               $where
               $order_by
            LIMIT $from, $count
         ";

			 global $RES_COUNT;
         global $wpdb;

         $RES_COUNT = $wpdb->get_var("
            SELECT
               count(p.ID)
            FROM {$wpdb->base_prefix}posts p
               $join
            WHERE p.post_status = 'publish'
               -- Only custom posts:
               AND p.post_type IN ('$in_posts')
               $where
         ");	

			return $wpdb->get_results($query);

	}
	function cps_ajax_search_results(){
		echo "<div id='cps_ajax_search_results'></div>";
	}
?>

Viewing all articles
Browse latest Browse all 11471

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>