Skip to content

Instantly share code, notes, and snippets.

@bobhiler
Created September 26, 2012 19:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobhiler/3790192 to your computer and use it in GitHub Desktop.
Save bobhiler/3790192 to your computer and use it in GitHub Desktop.
Swiftype Search Custom Modifications
<?php
/*
Plugin Name: Swiftype Search Custom Modifications
Description: This custom plugin lets you add additional parameters/filters to your Swifttype-powered searches
Author: Bob Hiler
Version: 0.0.1
*/
$topic = $_GET[ 'topic' ];
$topics_and_subtopics = array (
'find_your_idea'=>array ('keyword'=>'(\"customer interviews\") OR (MVP)','topiccategory'=>'toplevel','label'=>'Find Your Idea'),
'interview_customers'=>array ('keyword'=>'\"customer interviews\"','topiccategory'=>'find_your_idea','label'=>'Interview Customers'),
'minimal_viable_product'=>array ('keyword'=>'MVP','topiccategory'=>'find_your_idea','label'=>'Minimal Viable Products'),
);
function swiftype_search_params_filter($params) {
global $wp_query;
$params['filters[posts][object_type]'] = '!page';
$params['search_fields[posts][]'] = 'title^2.2';
$params['search_fields[posts][]'] = 'body';
// by default, make swiftype return posts in one of three categories. these numbers are the three category IDs we want to pull
$params['filters[posts][category]'] = array (17, 293, 244);
// if category is set to one of three pre-approved string values in the query string of the URL, then override the category filter so it only pulls that specific category
$category = $_GET['category'];
switch ($category) {
case 'interviews':
$params['filters[posts][category]'] = 17;
break;
case 'courses':
$params['filters[posts][category]'] = 244;
break;
case 'cheatsheets':
$params['filters[posts][category]'] = 293;
break;
}
// if sortby is set to 'date' in the query string of the URL, then tell swiftype to sort the results by date in descending order
$sortby = $_GET['sortby'];
if ( $sortby == 'date' ) {
$params['sort_field']['posts'] = 'timestamp';
$params['sort_direction']['posts'] = 'desc';
}
return $params;
}
function topic_to_keyword_conversion_pre_get_posts($query) {
global $topic, $topics_and_subtopics;
// Verify that we are on the search page that that this came from the event search form
if( $query->query_vars['s'] != '' && is_search() ) {
// Reset the search value
$query->set('s', $topics_and_subtopics[$topic]['keyword']);
}
}
// Filter the search page if $topic is set
if ( !empty($topic) ) {
add_filter('pre_get_posts', 'topic_to_keyword_conversion_pre_get_posts');
}
add_filter( swiftype_search_params, swiftype_search_params_filter, 8, 1 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment