Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude non-searchable post types from facet post types. #3386

Merged
merged 3 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion includes/classes/Feature/InstantResults/InstantResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,9 +835,13 @@ public function get_facets() {
$slug
);

$post_types = Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types();
$post_types = array_intersect( $post_types, $taxonomy->object_type );
$post_types = array_values( $post_types );

$facets[ $name ] = array(
'type' => 'taxonomy',
'post_types' => $taxonomy->object_type,
'post_types' => $post_types,
'labels' => array(
'admin' => $admin_label,
'frontend' => $labels->singular_name,
Expand Down
30 changes: 29 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 filter-instant-results-per-page',
'custom-instant-results-template open-instant-results-with-buttons filter-instant-results-per-page cpt-and-custom-tax',
'wpCli',
);
cy.login();
Expand Down Expand Up @@ -112,6 +112,7 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {
* Can change the URL when search term is changed
*/
it('Can see instant results elements, URL changes, reload, and update after changing search term', () => {
cy.activatePlugin('cpt-and-custom-tax', 'wpCli');
cy.maybeEnableFeature('instant-results');

cy.intercept({
Expand All @@ -121,6 +122,25 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {
},
}).as('apiRequest');

/**
* Add product category facet to test the labelling of facets
* with the same name.
*/
cy.intercept('**/wp-admin/admin-ajax.php*').as('ajaxRequest');
cy.visitAdminPage('admin.php?page=elasticpress');
cy.get('.ep-feature-instant-results .settings-button').click();
cy.get('.ep-feature-instant-results .components-form-token-field__input').type(
'cat{downArrow}{enter}',
);
cy.get('.ep-feature-instant-results .components-form-token-field__input').type(
'prod{downArrow}{enter}{esc}',
);
cy.get('.ep-feature-instant-results .button-primary').click();
cy.wait('@ajaxRequest');

/**
* Perform a search.
*/
cy.visit('/');

cy.get('.wp-block-search').last().as('searchBlock');
Expand All @@ -136,6 +156,14 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {
// Show the number of results
cy.get('@searchModal').find('.ep-search-results__title').contains(/\d+/);

/**
* The Category facet should specify its searchable post types.
*/
cy.get('.ep-search-panel__button').contains('Category').as('categoryFacet');
cy.get('@categoryFacet').should('contain', 'Posts');
cy.get('@categoryFacet').should('contain', 'Movies');
cy.get('@categoryFacet').should('not.contain', 'Albums');

cy.get('.ep-search-sidebar #ep-search-post-type-post')
.click()
.then(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace ElasticPress\Tests\E2e;

/**
* Create a CPT called "Movies".
* Create a CPT called "Movies" and a non-searchable CPT called "Group".
*/
function create_post_type() {
register_post_type(
Expand All @@ -24,6 +24,21 @@ function create_post_type() {
],
'public' => true,
'has_archive' => true,
'taxonomies' => [ 'category' ],
]
);

register_post_type(
'group',
[
'labels' => [
'name' => __( 'Albums' ),
'singular_name' => __( 'Album' ),
],
'exclude_from_search' => true,
'has_archive' => true,
'public' => true,
'taxonomies' => [ 'category' ],
]
);
}
Expand Down