Skip to content

Instantly share code, notes, and snippets.

@solepixel
Created November 14, 2016 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solepixel/0379857d5d7a18691efdcce8617a8f8e to your computer and use it in GitHub Desktop.
Save solepixel/0379857d5d7a18691efdcce8617a8f8e to your computer and use it in GitHub Desktop.
Algolia Search Implementation
<?php
/**
* Template function to output search results
*/
function display_listings(){
$algolia = Algolia_Plugin::get_instance();
$index = $algolia->get_api()->get_client()->initIndex( 'rpwp_posts_listing' );
$search = $index->search( '', mytheme_algolia_search_parameters() );
$listings = mytheme_algolia_get_posts( $search );
// loop goes here...
}
/**
* Seems like no matter what my parameters are, I get the same results.
*/
function mytheme_algolia_search_parameters(){
$params = array(
'facets' => array(
'status' => 'Active',
'listing_class' => 'RE_1'
)
);
return $params;
}
/**
* This converts Algolia search results to a WP_Query
*/
function mytheme_algolia_get_posts( $search ){
$post_ids = wp_list_pluck( $search['hits'], 'post_id' );
$args = array(
'post_type' => 'listing',
'post_status' => 'publish',
'posts_per_page' => $search['hitsPerPage'],
'post__in' => $post_ids
);
$query = new WP_Query( $args );
$query->found_posts = $search['nbHits'];
$query->max_num_pages = $search['nbPages'];
$query->paged = $search['page'];
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment