Skip to content

Instantly share code, notes, and snippets.

@yojance
Created August 12, 2015 12:22
Show Gist options
  • Save yojance/bfa97cc8315e7dd73d2c to your computer and use it in GitHub Desktop.
Save yojance/bfa97cc8315e7dd73d2c to your computer and use it in GitHub Desktop.
Advanced Search Foundation
<?php
class Advanced_Search {
public $version = 1;
public function __construct() {
// WordPress Hooks
$this->hooks();
}
public function hooks() {
add_action( 'pre_get_posts', array( $this, 'override_search_parameters' ), 5, 1 );
}
public function override_search_parameters( $query ) {
if( $query->is_main_query() && ! is_admin() ) {
if( $query->is_search ) {
$search_term = $query->get( 's' );
// Set these Post Types
$query->set( 'post_type', apply_filters( 'ko_search_post_types', array( 'sfwd-courses', 'magazine', 'discount', 'post', 'page' ) ) );
$args = array (
'role' => 'author',
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*'.esc_attr( $search_term ).'*',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'description',
'value' => $search_term ,
'compare' => 'LIKE'
)
)
);
$instructors = new WP_User_Query( $args );
if( count( $instructors->results >= 1 ) ) {
$query->set( 'instructors', $instructors->results );
}
$total_users = count( $instructors->results );
$query->set( 'posts_per_page', apply_filters( 'ko_search_posts_per_page', 100 - $total_users ) );
}
}
}
}
$advanced_search = new Advanced_Search();
include_once( 'lib/class-advanced-search.php' );
#search-filters {}
#search-filters ul { margin-left: 0px; }
#search-filters li { list-style-type: none; width: 16.6%; float: left; }
#search-filters {}
.button.filter { display: inline-block; width: 100%; zoom: 1; /* zoom and *display = ie7 hack for display:inline-block */
*display: inline; vertical-align: baseline; margin: 0 2px; outline: none; cursor: pointer; text-align: center; text-decoration: none;
padding: .5em 2em .55em; text-shadow: 0 1px 1px rgba(0,0,0,.3); /*
-webkit-border-radius: .5em;
-moz-border-radius: .5em;
border-radius: .5em; */
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.2); -moz-box-shadow: 0 1px 2px rgba(0,0,0,.2); box-shadow: 0 1px 2px rgba(0,0,0,.2);
}
.button.filter.white {
color: #606060; border: solid 1px #b7b7b7; background: #fff; background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#ededed));
background: -moz-linear-gradient(top, #fff, #ededed); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#ededed');
}
.button.filter.white:hover {
background: #ededed; background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#dcdcdc));
background: -moz-linear-gradient(top, #fff, #dcdcdc); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#dcdcdc');
}
.button.filter.white:active {
color: #999; background: -webkit-gradient(linear, left top, left bottom, from(#ededed), to(#fff));
background: -moz-linear-gradient(top, #ededed, #fff); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ededed', endColorstr='#ffffff');
}
.button.filter.orange {
color: #fef4e9; border: solid 1px #da7c0c; background: #f78d1d; background: -webkit-gradient(linear, left top, left bottom, from(#faa51a), to(#f47a20));
background: -moz-linear-gradient(top, #faa51a, #f47a20); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#faa51a', endColorstr='#f47a20');
}
.button.filter.orange:hover {
background: #f47c20; background: -webkit-gradient(linear, left top, left bottom, from(#f88e11), to(#f06015));
background: -moz-linear-gradient(top, #f88e11, #f06015); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#f88e11', endColorstr='#f06015');
}
.button.filter.orange:active {
color: #fcd3a5; background: -webkit-gradient(linear, left top, left bottom, from(#f47a20), to(#faa51a));
background: -moz-linear-gradient(top, #f47a20, #faa51a); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#f47a20', endColorstr='#faa51a');
}
<?php
/**
* The template for displaying search results pages.
*
* @package WordPress
* @subpackage Kelby_One
* @since KelbyOne 1.0
*/
get_header(); ?>
<section id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : ?>
<div class="container">
<div class="row">
<div class="col-md-12">
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'kelbyone' ), get_search_query() ); ?></h1>
</header><!-- .page-header -->
<div id="search-filters">
<ul>
<li class="search-filter"><a class="button white filter" href="#">Instructors</a></li>
<li class="search-filter"><a class="button white filter" href="#">Courses</a></li>
<li class="search-filter"><a class="button white filter" href="#">Magazines</a></li>
<li class="search-filter"><a class="button white filter" href="#">Discounts</a></li>
<li class="search-filter"><a class="button white filter" href="#">Pages</a></li>
<li class="search-filter"><a class="button white filter" href="#">Posts</a></li>
</ul>
</div>
<?php
// Start the loop.
while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>
</header><!-- .entry-header -->
</article><!-- #post-## -->
<?php endwhile; // End the loop.
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'kelbyone' ),
'next_text' => __( 'Next page', 'kelbyone' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'kelbyone' ) . ' </span>',
) );
// If no content, include the "No posts found" template.
else : ?>
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'kelbyone' ), get_search_query() ); ?></h1>
</header><!-- .page-header -->
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div>
</div>
</div>
</main><!-- .site-main -->
</section><!-- .content-area -->
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment