Skip to content

Commit

Permalink
Merge pull request #44 from WebDevStudios/feature/43-posts-per-page
Browse files Browse the repository at this point in the history
#43 Use filtered value of 'hitsPerPage' as 'posts_per_page' query param
  • Loading branch information
richaber authored Dec 27, 2019
2 parents d895de2 + 7a02583 commit abd3a02
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions includes/class-algolia-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,28 @@ public function pre_get_posts( WP_Query $query ) {
$current_page = get_query_var( 'page' );
}

$posts_per_page = (int) get_option( 'posts_per_page' );

/**
* Filters the array of parameters used in the Algolia Index search.
*
* @author WebDevStudios <contact@webdevstudios.com>
* @since 1.0.0
* @since 1.2.0 Introduced 'highlightPreTag' and 'highlightPostTag` parameters.
*
* @param array $params {
* Search parameters for the Algolia Index search.
*
* @type string $attributesToRetrieve Which attributes to retrieve.
* @type int $hitsPerPage Pagination parameter. The number of hits per page to retrieve.
* @type int $page Pagination parameter. The page of results to retrieve.
* @type string $highlightPreTag HTML string to insert before highlights in result snippets.
* @type string $highlightPostTag HTML string to insert after highlights in result snippets.
* }
*/
$params = apply_filters(
'algolia_search_params', array(
'algolia_search_params',
array(
'attributesToRetrieve' => 'post_id',
'hitsPerPage' => $posts_per_page,
'hitsPerPage' => (int) get_option( 'posts_per_page' ),
'page' => $current_page - 1, // Algolia pages are zero indexed.
'highlightPreTag' => '<em class="algolia-search-highlight">',
'highlightPostTag' => '</em>',
Expand Down Expand Up @@ -103,7 +119,7 @@ public function pre_get_posts( WP_Query $query ) {
$post_ids = array( 0 );
}

$query->set( 'posts_per_page', $posts_per_page );
$query->set( 'posts_per_page', $params['hitsPerPage'] );
$query->set( 'offset', 0 );

$post_types = 'any';
Expand Down

0 comments on commit abd3a02

Please sign in to comment.