Skip to content

Instantly share code, notes, and snippets.

@keithdevon
Last active January 25, 2024 12:51
Show Gist options
  • Save keithdevon/5624e38f775c61ee3e36 to your computer and use it in GitHub Desktop.
Save keithdevon/5624e38f775c61ee3e36 to your computer and use it in GitHub Desktop.
Relevanssi job search
// filter relevanssi based on job_region query
add_filter('relevanssi_modify_wp_query', 'region_tax_query');
function region_tax_query($query) {
$tax_query = array();
if (!empty($query->query_vars['job_region'])) {
$tax_query[] = array(
'taxonomy' => 'region',
'field' => 'id',
'terms' => $query->query_vars['job_region']
);
}
if (!empty($tax_query)) {
$tax_query['relation'] = 'AND'; // you can also use 'OR' here
$query->set('tax_query', $tax_query);
}
return $query;
}
add_filter('relevanssi_search_ok', 'search_trigger');
function search_trigger($search_ok) {
global $wp_query;
if (!empty($wp_query->query_vars['job_region'])) {
$search_ok = true;
}
return $search_ok;
}
add_action('relevanssi_hits_filter', 'allow_null_search');
function allow_null_search($hits) {
global $wp_query;
if(!empty($wp_query->query_vars['job_region'])) $region = $wp_query->query_vars['job_region'];
if(!empty($wp_query->query_vars['post_type'])) $post_type = $wp_query->query_vars['post_type'];
if ( $hits[0] == null ) {
// no search hits, so must create new
$args = array(
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => 'region',
'field' => 'term_id',
'terms' => array($region)
)
),
'posts_per_page' => -1
);
$hits[0] = get_posts( $args );
} else {
// posts available, take only those that match the conditions
$ok = array();
foreach ( $hits[0] as $hit ) {
if (is_singular($post_type) && has_term( $region, 'region' ) )
array_push( $ok, $hit );
}
$hits[0] = $ok;
}
return $hits;
}
<?php
global $division_slug;
if($division_slug = 'personnel') $job_post_type = 'vgc_personnel_jobs';
elseif($division_slug = 'labour') $job_post_type = 'vgc_jobs';
?>
<div class="widget">
<h3>Job search</h3>
<form role="search" method="get" id="job-search-form" class="form-job-search" action="<?php echo home_url('/'); ?>">
<div class="form-group">
<label class="hide" for="job-search-input"><?php _e('Search for:', 'roots'); ?></label>
<input type="text" value="<?php if (is_search()) { echo get_search_query(); } ?>" name="s" id="job-search-input" class="search-query form-control" placeholder="<?php _e('Search', 'roots'); ?> <?php bloginfo('name'); ?>">
</div>
<input type="hidden" name="post_type" value="<?php echo $job_post_type; ?>" />
<?php
wp_dropdown_categories(array('name' => 'job_region', 'taxonomy' => 'region', 'class' => 'form-control', 'show_option_all' => 'Search all regions'));
?>
<input type="submit" id="job-search-submit" class="btn btn-primary btn-block" value="<?php _e('Search our jobs', 'roots'); ?>" />
</form>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment