Updated search page

Does no longer query EVERYTHING with an empty search for and shows messages for no results and no params.
This commit is contained in:
Tetrakern 2023-08-06 17:54:39 +02:00
parent ba9ba189dd
commit bc8210abb3
8 changed files with 67 additions and 6 deletions

View File

@ -788,9 +788,20 @@ Fires right after the search form has been rendered.
---
### `do_action( 'fictioneer_search_no_params' )`
Fires right after opening the articles no-params `<section>` container in the `search.php` template.
**Hooked actions:**
* `fictioneer_no_search_params()` HTML for no search params. Priority 10.
---
### `do_action( 'fictioneer_search_no_results' )`
Fires right after opening the articles no-results `<section>` container in the `search.php` template.
**Hooked actions:**
* `fictioneer_no_search_results()` HTML for no search results. Priority 10.
---
### `do_action( 'fictioneer_site', $args )`

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -229,6 +229,12 @@ function fictioneer_extend_search_query( $query ) {
return;
}
// Empty search if no params provided
if ( empty( array_filter( $_GET ) ) ) {
$query->set( 'post__in', [0] );
return;
}
// Fix broken search if no term was entered
if ( empty( $query->get( 's' ) ) ) {
$query->set( 's', ' ' ); // Most posts should have at least one whitespace

View File

@ -493,4 +493,30 @@ add_action( 'fictioneer_collections_after_content', 'fictioneer_sort_order_filte
add_action( 'fictioneer_recommendations_after_content', 'fictioneer_sort_order_filter_interface', 20 );
add_action( 'fictioneer_archive_loop_before', 'fictioneer_sort_order_filter_interface', 10 );
// =============================================================================
// SEARCH RESULTS
// =============================================================================
/**
* Outputs the HTML for no search params
*
* @since Fictioneer 5.5.2
*/
function fictioneer_no_search_params() {
echo __( 'Please enter search parameters.', 'fictioneer' );
}
add_action( 'fictioneer_search_no_params', 'fictioneer_no_search_params' );
/**
* Outputs the HTML for no search results
*
* @since Fictioneer 5.5.2
*/
function fictioneer_no_search_results() {
echo __( 'No results.', 'fictioneer' );
}
add_action( 'fictioneer_search_no_results', 'fictioneer_no_search_results' );
?>

View File

@ -14,6 +14,7 @@ global $wp_query;
// Setup
$count = $wp_query->found_posts;
$no_params = empty( array_filter( $_GET ) );
$post_type = $_GET['post_type'] ?? 'any';
$sentence = $_GET['sentence'] ?? '0';
@ -101,7 +102,7 @@ $hook_args = array(
do_action( 'fictioneer_search_form_after' );
?>
<?php if ( have_posts() ) : ?>
<?php if ( have_posts() && ! $no_params ) : ?>
<section class="search-results__content">
<ul class="card-list _no-mutation-observer" id="search-result-list">
<?php
@ -128,8 +129,10 @@ $hook_args = array(
</ul>
<nav class="pagination _padding-top"><?php echo fictioneer_paginate_links( $pag_args ); ?></nav>
</section>
<?php elseif ( $no_params ) : ?>
<section class="search-results__content _no-params"><?php do_action( 'fictioneer_search_no_params' ); ?></section>
<?php else : ?>
<section class="search-results__no-results"><?php do_action( 'fictioneer_search_no_results' ); ?></section>
<section class="search-results__content _no-results"><?php do_action( 'fictioneer_search_no_results' ); ?></section>
<?php endif; ?>
<footer class="search-results__footer"><?php do_action( 'fictioneer_search_footer' ); ?></footer>

View File

@ -16,6 +16,7 @@
<?php
// Setup
$no_params = empty( array_filter( $_GET ) );
$simple_mode = isset( $args['simple'] ) && $args['simple'];
$cache_mode = isset( $args['cache'] ) && $args['cache'];
$show_advanced = ! get_option( 'fictioneer_disable_theme_search' ) && ! $simple_mode;
@ -83,12 +84,19 @@ if ( ! $skip_author_keywords ) {
method="get"
class="search-form <?php if ( ! $show_advanced ) echo '_simple'; ?>"
action="<?php echo esc_url( home_url( '/' ) ); ?>"
data-advanced="false"
data-advanced="<?php echo ( $show_advanced && $no_params ) ? 'true' : 'false'; ?>"
>
<?php if ( $show_advanced ) : ?>
<div class="allow-list" hidden><?php echo json_encode( $allow_list ); ?></div>
<input type="checkbox" class="search-form__advanced-control" id="<?php echo $toggle_id; ?>" autocomplete="off" hidden>
<input
type="checkbox"
class="search-form__advanced-control"
id="<?php echo $toggle_id; ?>"
autocomplete="off"
checked="<?php echo ( $show_advanced && $no_params ) ? 'checked' : ''; ?>"
hidden
>
<?php endif; ?>
<div class="search-form__bar">

View File

@ -197,5 +197,12 @@
&__content {
margin-top: 2rem;
&:is(._no-results, ._no-params) {
font: var(--font-weight-strong) var(--fs-ds)/1.5 var(--ff-note);
text-align: center;
overflow-wrap: break-word;
opacity: 0.25;
}
}
}