Skip to content

Commit

Permalink
Merge pull request #3292 from 10up/feature/3274
Browse files Browse the repository at this point in the history
Allow users to filter the number of results per page in Instant Results
  • Loading branch information
felipeelia authored Feb 8, 2023
2 parents dfd6c76 + 57de352 commit 0af7fe3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"wp-content/plugins/shorten-autosave.php": "./tests/cypress/wordpress-files/test-plugins/shorten-autosave.php",
"wp-content/plugins/fake-log-messages.php": "./tests/cypress/wordpress-files/test-plugins/fake-log-messages.php",
"wp-content/plugins/enable-debug-bar.php": "./tests/cypress/wordpress-files/test-plugins/enable-debug-bar.php",
"wp-content/plugins/filter-instant-results-per-page.php": "./tests/cypress/wordpress-files/test-plugins/filter-instant-results-per-page.php",
"wp-content/uploads/content-example.xml": "./tests/cypress/wordpress-files/test-docs/content-example.xml"
}
}
Expand Down
13 changes: 13 additions & 0 deletions docs/theme-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,16 @@ document.getElementById('search-blocks').addEventListener('click', () => {
});
</script>
```

### Change the number of results per page

As of ElasticPress 4.5.0 Instant Results will display the same number of results per page as the number of posts per page set in _Settings > Reading_. The `ep_instant_results_per_page` filter can be used to change the number of results per page independently of the number of posts per page:

```
add_filter(
'ep_instant_results_per_page',
function() {
return 12;
}
);
```
12 changes: 11 additions & 1 deletion includes/classes/Feature/InstantResults/InstantResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function __construct() {
'facets' => 'post_type,category,post_tag',
'match_type' => 'all',
'term_count' => '1',
'per_page' => get_option( 'posts_per_page', 6 ),
];

$settings = $this->get_settings() ? $this->get_settings() : array();
Expand Down Expand Up @@ -945,6 +946,15 @@ public function get_facets_for_admin() {
* @return array Search args schema.
*/
public function get_args_schema() {
/**
* The number of resutls per page for Instant Results.
*
* @since 4.5.0
* @hook ep_instant_results_per_page
* @param {int} $per_page Results per page.
*/
$per_page = apply_filters( 'ep_instant_results_per_page', $this->settings['per_page'] );

$args = array(
'highlight' => array(
'type' => 'string',
Expand All @@ -967,7 +977,7 @@ public function get_args_schema() {
),
'per_page' => array(
'type' => 'number',
'default' => 6,
'default' => absint( $per_page ),
),
'post_type' => array(
'type' => 'strings',
Expand Down
35 changes: 34 additions & 1 deletion tests/cypress/integration/features/instant-results.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {

beforeEach(() => {
cy.deactivatePlugin(
'custom-instant-results-template open-instant-results-with-buttons',
'custom-instant-results-template open-instant-results-with-buttons filter-instant-results-per-page',
'wpCli',
);
cy.login();
Expand Down Expand Up @@ -155,6 +155,39 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {
cy.get('#querylist').should('be.visible');
});

it('Is possible to filter the number of results', () => {
/**
* The number of results should match the posts per page
* setting by default.
*/
cy.maybeEnableFeature('instant-results');
cy.visitAdminPage('options-reading.php');
cy.get('input[name="posts_per_page"]').then(($input) => {
const perPage = $input.val();

cy.visit('/');
cy.get('.wp-block-search').last().as('searchBlock');
cy.get('@searchBlock').find('input[type="search"]').type('block');
cy.get('@searchBlock').find('button').click();
cy.url().should('include', `per_page=${perPage}`);

/**
* Activate test plugin with filter.
*/
cy.activatePlugin('filter-instant-results-per-page', 'wpCli');

/**
* On searching the per_page parameter should reflect the
* filtered value.
*/
cy.visit('/');
cy.get('.wp-block-search').last().as('searchBlock');
cy.get('@searchBlock').find('input[type="search"]').type('block');
cy.get('@searchBlock').find('button').click();
cy.url().should('include', 'per_page=3');
});
});

it('Can filter results by price', () => {
/**
* Add price range facet.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Plugin Name: Filter Instant Results Per Page
* Description: Filters the number of Instant Results per page for test purposes.
* Version: 1.0.0
* Author: 10up Inc.
* License: GPLv2 or later
*/

add_filter( 'ep_instant_results_per_page', function() { return 3; } );

0 comments on commit 0af7fe3

Please sign in to comment.