Skip to content

Instantly share code, notes, and snippets.

@Hube2
Created January 25, 2017 22:59
Show Gist options
  • Save Hube2/107d9509d7a52f354879fb099fe9f48c to your computer and use it in GitHub Desktop.
Save Hube2/107d9509d7a52f354879fb099fe9f48c to your computer and use it in GitHub Desktop.
Simple SEO/User friendly search
<?php
function excel_case_study_results($posts) {
/*
this function is called in the content area of
either the post type archive template or
the either taxonomy archive template like this
if (have_posts()) {
?>
<div id="case-study-list">
<?php excel_case_study_results($posts); ?>
</div>
<div id="case-study-load-more-div" class="case-study-load-more" style="visibility: hidden;">
<a href="#" id="case-study-load-more-a">Load More</a>
</div>
<?php
} else {
?><div id="case-study-list no-results"><p>No Results Found</p></div><?php
}
in addition to this there is a JS file added here to loading more results
*/
global $post;
$count = 0;
$row_closed = true;
foreach ($posts as $post) {
setup_postdata($post);
if (($count % 2) == 0) {
$row_closed = false;
?>
<div class="row">
<?php
}
$classes = array('col-lg-6', 'col-md-6', 'col-sm-6', 'col-xs-12');
$title = get_the_title();
if (get_field('title')) {
$title = get_field('title');
}
$background = NULL;
$image_id = get_field('featured_image', false, false);
if ($image_id) {
$image = wp_get_attachment_image_src($image_id, 'persona-xl');
if ($image) {
$background = $image[0];
}
}
?>
<article id="post-<?php the_ID(); ?>" <?php post_class($classes); ?>>
<a href="<?php the_permalink(); ?>" <?php if ($background) { echo 'style="background-image:url(' . $background . ')";'; } ?>>
<div class="item-excerpt">
<h2><?php the_title(); ?></h2>
<p class="excerpt"><?php the_field('excerpt'); ?></p>
<p class="learn-more"><?php
if (get_field('has_video')) {
?><i class="fa fa-play-circle"></i><?php
}
?>View Case Study</p>
</div>
</a>
</article>
<?php
if (($count % 2) != 0) {
$row_closed = true;
?>
</div>
<?php
}
$count++;
} // end foreach post
if (!$row_closed) {
echo '</div>';
}
wp_reset_postdata();
} // end function excel_case_study_results
add_filter('pre_get_posts', 'case_study_pre_get_posts');
function case_study_pre_get_posts($query) {
/*
this is the pre_get_posts filter for this section
you can see that if we're doing ajax then it uses the post values
but if ajax is not being done it does not alter the query.
*/
if (defined('DOING_AJAX') && DOING_AJAX &&
isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'case-study') {
$tax_query = array();
if (isset($_GET['case-study-solution']) && $_GET['case-study-solution'] != '') {
$tax_query[] = array(
'taxonomy' => 'solution',
'terms' => array(intval($_GET['case-study-solution']))
);
}
if (isset($_GET['case-study-industry']) && $_GET['case-study-industry'] != '') {
$tax_query[] = array(
'taxonomy' => 'industry',
'terms' => array(intval($_GET['case-study-solution']))
);
}
if (count($tax_query)) {
$tax_query['relation'] = 'AND';
$query->set('tax_query', $tax_query);
}
if (isset($_GET['case-study-page'])) {
$query->set('paged', intval($_GET['case-study-page']));
}
} elseif (is_admin()) {
return;
}
if (isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'case-study') {
$query->set('posts_per_page', 6);
if (!isset($_GET['case-study-page'])) {
$query->set('paged', 1);
}
$query->set('orderby', array('memu_order' => 'ASC', 'title' => 'ASC'));
}
} // end case_study_pre_get_posts
<?php
/*
This is the search form.
The same search form is used in the post type archive template
the taxonomy archive templates in two taxonomies
and this file is loaded using:
get_template_part('template-parts/form', 'case-study-search');
*/
$queried_object = get_queried_object();
$current_tax = false;
$current_term = false;
if (isset($queried_object->term_id)) {
$current_tax = $queried_object->taxonomy;
$current_term = $queried_object->term_id;
}
?>
<div id="case-studies-search-bar">
<p class="search-bar-text hidden-xs">
<?php the_field('search_bar_text', 141); ?>
</p>
<form id="case-studies-search" name="case-studies-search" action="">
<span class="form-label">Filter By:</span>
<input type="hidden" name="case-study-page" id="case-study-page" value="<?php
if ($wp_query->max_num_pages == 0) {
echo 0;
} else {
echo 1;
}
?>" />
<input type="hidden" name="case-study-pages" id="case-study-pages" value="<?php echo $wp_query->max_num_pages; ?>" />
<?php
// solutions
$tax = 'solution';
$args = array(
'taxonomy' => $tax,
'hide_empty' => false
);
$terms = get_terms($args);
if (count($terms)) {
?>
<select name="case-study-solution" id="case-study-solution" class="selectpicker">
<option value=""<?php
if ($current_tax != $tax || ($current_tax == $tax && $current_term != $term->term_id)) {
?> selected="selected"<?php
}
?>>Solutions</option>
<?php
foreach ($terms as $term) {
?>
<option value="<?php echo $term->term_id; ?>"<?php
if ($current_tax == $tax && $current_term == $term->term_id) {
?> selected="selected"<?php
}
?>><?php echo $term->name; ?></option>
<?php
}
?>
</select>
<?php
}
// industries
$tax = 'industry';
$args = array(
'taxonomy' => $tax,
'hide_empty' => false
);
$terms = get_terms($args);
if (count($terms)) {
?>
<select name="case-study-industry" id="case-study-industry" class="selectpicker">
<option value=""<?php
if ($current_tax != $tax || ($current_tax == $tax && $current_term != $term->term_id)) {
?> selected="selected"<?php
}
?>>Industries</option>
<?php
foreach ($terms as $term) {
?>
<option value="<?php echo $term->term_id; ?>"<?php
if ($current_tax == $tax && $current_term == $term->term_id) {
?> selected="selected"<?php
}
?>><?php echo $term->name; ?></option>
<?php
}
?>
</select>
<?php
}
?>
</form>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment