Created
November 14, 2016 14:44
-
-
Save solepixel/0379857d5d7a18691efdcce8617a8f8e to your computer and use it in GitHub Desktop.
Algolia Search Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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