Custom Search for WordPress

<? // template name: Custom Search ?>

<?php
	
	


	
function score_the_title( $title, $search_terms = null ) {
	$each_search_term    = explode(" ",$search_terms);
	$score               = 0;
	if (strpos($title, $search_terms) !== false) { $score = $score + 100; }
	
	if (!empty($each_search_term)) { foreach($each_search_term as $this_term) { 
		if (strpos($title, $this_term) !== false) { $score = $score + 50; }
	} }
	
	return $score;
	
}	

function score_the_tags( $post_id, $search_terms = null ) {
	$each_search_term    = explode(" ",$search_terms);
	$score               = 0;
	
	if (!empty($each_search_term)) { foreach($each_search_term as $this_term) { 
		if( has_tag($this_term, $post_id) ){ $score = $score + 25; }
	} }
	
	return $score;
	
}
	
	$search_terms        = strtolower($_GET['search_terms']);
	$post_types          = $_GET['post_types']; if(empty($post_types)) { $post_types = 'courses'; } 
	$all_search_results  = array(); 
	
	
    $call_args = array(
        'post_type'        => $post_types,
        'posts_per_page'   => -1,
    );
    $custom_query = new WP_Query( $call_args ); 
    if ( $custom_query->have_posts() ) { while ( $custom_query->have_posts() ) { $custom_query->the_post();
	    $total_post_score   = 0;
	    $post_id            = get_the_ID();
	    $post_title         = strtolower(get_the_title()); 
	    $post_title_upper   = get_the_title();
	    $title_score        = score_the_title( $post_title, $search_terms );
	    $tag_score          = score_the_tags( $post_id, $search_terms );
	    
	    $total_post_score   = $title_score + $tag_score;
	    if(!empty($total_post_score)) { 
		    $data['title']                  = "$post_title_upper";
		    $data['post_id']                = "$post_id";
		    $data['featured_image']         = wp_get_attachment_image_src( get_post_thumbnail_id($post_id), 'full')[0];
		    $data['permalink']              = get_the_permalink($post_id);
		    $data['title_score']            = $title_score;
		    $data['tag_score']              = $tag_score;
		    $data['total_post_score']       = $total_post_score;
		    $data['difficulty']             = get_field( 'difficulty',$post_id );
		    $data['duration']               = get_field( 'duration',$post_id );
		    $this_post_excerpt              = get_the_content();
		    $this_post_excerpt              = do_shortcode($this_post_excerpt);
		    $this_post_excerpt              = strip_tags($this_post_excerpt);
		    
		    if( have_rows('extended_flex',$post_id) ) { while ( have_rows('extended_flex',$post_id) ) { the_row();
				if( get_row_layout() == 'course_overview' ) {
		            $this_post_excerpt     = strip_tags(get_sub_field('summery'));  
		        }
		    }}
		    
		    if (function_exists('string_limit_words')) { $data['post_excerpt'] = string_limit_words($this_post_excerpt,25); }
		    $all_search_results[$post_id]   =  $data;
		    
		    
	    }
	    
	}}


?>



<style>
.no_header_image .header_with_overlay_wrap {
    min-height: 310px;
}
.has_bg_color .hwo_text_block {
    top: calc(50% + 30px);
}
.hwo_text_block {
    width: 100%;
    top: calc(50% + 75px);
    transform: translate(0%, -50%);
    position: absolute;
    z-index: 10;
}
.no_header_image .hwo_title {
    font-size: 50px !important;
    font-family: 'Akzidenz Grotesk Regular';
}
.hwo_title {
    font-size: 80px;
    font-family: 'Akzidenz Grotesk Bold';
    line-height: 1em;
    color: #ffffff;
    margin-bottom: 10px;
}
.flex_start {
    display: flex;
    flex-flow: row wrap;
    justify-content: flex-start;
}








        .single_courses {
	        width: calc(25% - 11.3px);
	        height: 480px;
	        margin-right: 15px;
 
		    position: relative;
		    overflow: hidden;
		    z-index: 10;
		    margin-bottom: 15px;
        }
        .single_courses:nth-child(4n) {
	        margin-right: 0px;
        }
		.coursess_featured_wrap {
		    width: 100%;
		    height: 360px;
		}
		.single_courses.itw_popup {
		    position: absolute;
		    top: 120%;
		    left: 0px;
		    transition: all .5s ease;
		    width: 100%;
		    height: 50%;
		}
		.single_courses {
		    background-color: #ffffff;
		}
		.courses_post_title {
			font-size: 20px;
            font-family: 'Akzidenz Grotesk Regular';
		}
		.courses_text_wrap {
		    padding: 16px 24px;
		    min-height: 110px;
			background: #f7f8f9;
		}
		.single_courses:hover .courses_text_wrap.itw_popup {
		    bottom: 0%;
		    opacity: 1;
		    transition: all .5s ease;
		}
		.courses_text_wrap.itw_popup {
			position: absolute;
			bottom: -100%;
			left: 0px;
			background-color: #ffffff;
			border: 1px solid #f7f8f9;
		    transition: all .5s ease;
		    width: 100%;
		}
		.courses_link {
			text-align: center;
		}
		.courses_text_wrap.itw_popup .courses_post_title {
			margin-bottom: 10px;
		}

		.courses_excerpt {
		    margin-bottom: 24px;
		    color: #9e9e9e;
		}
		.tag_wrap {
			padding: 25px 0;
		}
		.tag_wrap .cat_tag {
			background-color: #000000;
		}
		.single_courses button.solid_yellow:hover {
		    background-color: rgba(0,0,0,0.0);
		}
		.single_term {
			margin-right: 21px;
            font-family: 'Akzidenz Grotesk Regular';
		    font-size: 14px;
		    line-height: 1.42857;
		    color: #777;
		    text-transform: capitalize;
		    padding: 10px 0;
		    cursor: pointer;
		}
		.single_term:hover {
            color: var(--blue);
		}
		.filter_active .single_term {
            color: var(--blue);
		}
		.pcfm_filter_block {
			background: #f9f9f9;
		}
		header.header_2 .menu-item-has-children .the_x:after {
	 
		}
		.course_feed_block {
			padding: 0 25px;
		}
		.pcfm_filter_block {
			padding: 0 25px;
		}
		.search_wrap {
			position: absolute;
			right: 0px;
			top: -55px;
		}
		#search_courses_1 {
		    border: 2px solid var(--yellow);
		    padding: 10px 25px;
		    border-radius: 30px;
		}
		.search_wrap.icon-search:before {
		    color: var(--yellow) !important;
		    position: absolute;
		    top: 14px;
		    right: 10px;
		    z-index: 10;
		}
		.courses_link .solid_yellow {
			margin: 0px;
		}

		@media screen and (max-width: 900px) {
			 .single_courses {
				 width: calc(50% - 10px);
				 margin-right: 0px;
			 }
			 .feed_block_wrap.flex_start {
				 display: flex;
				 flex-flow: row wrap;
				 justify-content: space-between;
			 }
		}
		@media screen and (max-width: 600px) {
			 .single_courses {
				 width: 100%;
				 margin-right: 0px;
			 }
		}
</style>

<?php get_header(); ?>

<? include TEMPLATEPATH.'/functions/global_variables.php'; ?>

		<div class="content_block">
		
		    <div class="content_wrapper">                               
		       
		        <div class="middle_column">
			        
			        
					<div class="header_with_overlay_block header_with_overlay_block_0 hwo_full_width no_header_image hwo_text_left module bg_yellow has_bg_color" style="background-image: url();">
						<div class="header_with_overlay_wrap header_with_overlay_wrap_0">
									<div class="hwo_text_block">
							    <div class="hwo_text_wrap page_width">
								    <div class="inner_text_block">
									    <div class="center_text">					     
										    <div class="hwo_title" id="hwo_title_1">Search results for "<?php echo $_GET['search_terms']; ?>"</div>					    					    
									    </div>
									</div>
							    </div>
						    </div>
						</div>
					</div>
					
					
					
					<div class='course_feed_block course_feed_block_block_1'>
						<div class='course_feed_block_wrap course_feed_block_wrap_1 page_width'>		
							<div class='tag_wrap'><div class='cat_tag'>courses<span id='sub_tag_filter_1'></span></div></div>
				 	        <div id='course_feed_1' class='feed_block_wrap flex_start'>	
								<?php
								if (!empty($all_search_results)) { 
									sortBySubkey($all_search_results, 'total_post_score', SORT_DESC);
									foreach($all_search_results as $this_result) { 
										//echo $this_result['title']." ".$this_result['total_score']." <br />";
										if(empty($this_result['featured_image'])) { continue; }
										?>
										
										
							        	<div class='single_courses' data-total_score='<?php echo $this_result['total_post_score']; ?>' data-title_score='<?php echo $this_result['title_score']; ?>' data-tag_score='<?php echo $this_result['tag_score']; ?>'>
								    	    <div class='coursess_featured_wrap backg' style='background-image: url(<?php echo $this_result['featured_image']; ?>);'></div>
								    	    <div class='courses_text_wrap'>
								    	        <div class='courses_post_title'><div class='ipt_control'><?php echo $this_result['title']; ?></div></div>
						 		    	    </div>
								    	    <div class='courses_text_wrap itw_popup'>
								    	        <div class='courses_post_title'><div class='ipt_control'><?php echo $this_result['title']; ?></div></div>
								    	        <div class='courses_excerpt'><?php echo $this_result['post_excerpt']; ?></div>
								    	        <div class='courses_link'>
										            <a href='<?php echo $this_result['permalink']; ?>' target='_self'><button class='solid_yellow' >LEARN MORE</button></a>
								    	        </div>
								   
											    <?php if(!empty($difficulty) || !empty($duration)) { ?>
									    	        <div class='courses_details flex'>
									    	            <div class='difficulty'><?php echo $this_result['difficulty']; ?></div>
									    	            <div class='duration'><?php echo $this_result['duration']; ?></div>
									    	        </div>
											    <?php } ?>
			   
						                    </div>
			    	                    </div>
	
										<?php
									} 								
								}							
								?>
					        </div>
		    	        </div>
		    	    </div>



		        </div>
		
		    </div> 				    
		    
		</div> 
	
	    <?php get_footer(); ?>
	       
	</body>

</html>
Comments