Add story status select to advanced search

This commit is contained in:
Tetrakern 2024-02-15 23:56:21 +01:00
parent beac8fa655
commit 28a5630f8b
5 changed files with 88 additions and 3 deletions

View File

@ -847,6 +847,17 @@ Fires right after the search form has been rendered.
---
### `do_action( 'fictioneer_search_form_filters', $args )`
Fires after the search form fields for type, match, sort, and order have been rendered. This allows you to render additional fields. Note that any additional fields must also be manually added to the query, see `_search.php` partial.
**Parameters:**
* $args (array) Arguments passed to the search form.
**Hooked actions:**
* `fictioneer_add_search_for_status()` HTML for story status select. Priority 10.
---
### `do_action( 'fictioneer_search_no_params' )`
Fires right after opening the articles no-params `<section>` container in the `search.php` template.

View File

@ -180,6 +180,7 @@ if ( ! function_exists( 'fcn_keyword_search_authors_input' ) ) {
* Extend search query with custom input
*
* @since 5.0.0
* @since 5.11.0 - Added meta query for story status.
*
* @param WP_Query $query The query.
*/
@ -221,6 +222,12 @@ function fictioneer_extend_search_query( $query ) {
$ex_warnings = empty( $_GET['ex_warnings'] ) ? [] : array_map( 'absint', explode( ',', $_GET['ex_warnings'] ) );
$ex_tags = empty( $_GET['ex_tags'] ) ? [] : array_map( 'absint', explode( ',', $_GET['ex_tags'] ) );
$story_status = array_intersect(
[ $_GET['story_status'] ?? 0 ],
['Completed', 'Ongoing', 'Oneshot', 'Hiatus', 'Canceled']
);
$story_status = reset( $story_status ) ?: 0;
// Exclude pages if necessary
if ( $is_any_post || empty( $_GET['post_type'] ) ) {
$query->set( 'post_type', ['post', 'fcn_story', 'fcn_chapter', 'fcn_collection', 'fcn_recommendation'] );
@ -384,6 +391,19 @@ function fictioneer_extend_search_query( $query ) {
if ( ! empty( $authors ) ) {
$query->set( 'author', implode( ',', $authors ) );
}
// Meta query
if ( $story_status ) {
$meta_query = array(
array(
'key' => 'fictioneer_story_status',
'value' => $story_status,
'compare' => '='
)
);
$query->set( 'meta_query', $meta_query );
}
}
if ( ! get_option( 'fictioneer_disable_theme_search' ) ) {

View File

@ -621,9 +621,40 @@ add_action( 'fictioneer_recommendations_after_content', 'fictioneer_sort_order_f
add_action( 'fictioneer_archive_loop_before', 'fictioneer_sort_order_filter_interface', 10 );
// =============================================================================
// SEARCH RESULTS
// SEARCH FORM & RESULTS
// =============================================================================
/**
* Adds story status select to advanced search form
*
* @since 5.11.0
*
* @param array $args Arguments passed to the search form.
*/
function fictioneer_add_search_for_status( $args ) {
$story_status = array_intersect(
[ $_GET['story_status'] ?? 0 ],
['Completed', 'Ongoing', 'Oneshot', 'Hiatus', 'Canceled']
);
$story_status = reset( $story_status ) ?: 0;
// Start HTML ---> ?>
<div class="search-form__select-wrapper select-wrapper">
<div class="search-form__select-title"><?php _ex( 'Status', 'Advanced search heading.', 'fictioneer' ); ?></div>
<select name="story_status" class="search-form__select" autocomplete="off" data-default="Any">
<option value="Any" <?php echo ! $story_status ? 'selected' : ''; ?>><?php _ex( 'Any', 'Advanced search option.', 'fictioneer' ); ?></option>
<option value="Completed" <?php echo $story_status === 'Completed' ? 'selected' : ''; ?>><?php echo fcntr( 'Completed' ); ?></option>
<option value="Ongoing" <?php echo $story_status === 'Ongoing' ? 'selected' : ''; ?>><?php echo fcntr( 'Ongoing' ); ?></option>
<option value="Oneshot" <?php echo $story_status === 'Oneshot' ? 'selected' : ''; ?>><?php echo fcntr( 'Oneshot' ); ?></option>
<option value="Hiatus" <?php echo $story_status === 'Hiatus' ? 'selected' : ''; ?>><?php echo fcntr( 'Hiatus' ); ?></option>
<option value="Canceled" <?php echo $story_status === 'Canceled' ? 'selected' : ''; ?>><?php echo fcntr( 'Canceled' ); ?></option>
</select>
</div>
<?php // <--- End HTML
}
add_action( 'fictioneer_search_form_filters', 'fictioneer_add_search_for_status' );
/**
* Outputs the HTML for no search params
*

View File

@ -5,6 +5,7 @@
* @package WordPress
* @subpackage Fictioneer
* @since 5.0.0
* @since 5.11.0 - Consider querying for story status.
*/
?>
@ -21,6 +22,12 @@ $sentence = sanitize_text_field( $_GET['sentence'] ?? 0 );
$order = sanitize_text_field( $_GET['order'] ?? 'desc' );
$orderby = sanitize_text_field( $_GET['orderby'] ?? 'modified' );
$story_status = array_intersect(
[ $_GET['story_status'] ?? 0 ],
['Completed', 'Ongoing', 'Oneshot', 'Hiatus', 'Canceled']
);
$story_status = reset( $story_status ) ?: 0;
$queried_genres = sanitize_text_field( $_GET['genres'] ?? 0 );
$queried_fandoms = sanitize_text_field( $_GET['fandoms'] ?? 0 );
$queried_characters = sanitize_text_field( $_GET['characters'] ?? 0 );
@ -39,6 +46,7 @@ $hook_args = array(
'sentence' => $sentence,
'order' => $order,
'orderby' => $orderby,
'story_status' => $story_status,
'queried_genres' => $queried_genres,
'queried_fandoms' => $queried_fandoms,
'queried_characters' => $queried_characters,
@ -52,7 +60,7 @@ $hook_args = array(
);
// Advanced search?
$is_advanced_search = $post_type != 'any' || $sentence != '0' || $order != 'desc' || $orderby != 'modified' || $queried_tags || $queried_genres || $queried_fandoms || $queried_characters || $queried_warnings || $queried_ex_tags || $queried_ex_genres || $queried_ex_fandoms || $queried_ex_characters || $queried_ex_warnings;
$is_advanced_search = $post_type != 'any' || $sentence != '0' || $order != 'desc' || $orderby != 'modified' || $queried_tags || $queried_genres || $queried_fandoms || $queried_characters || $queried_warnings || $queried_ex_tags || $queried_ex_genres || $queried_ex_fandoms || $queried_ex_characters || $queried_ex_warnings || $story_status;
$hook_args['is_advanced_search'] = $is_advanced_search;

View File

@ -5,6 +5,7 @@
* @package WordPress
* @subpackage Fictioneer
* @since 5.0.0
* @since 5.11.0 - Added 'fictioneer_search_form_filters' action hook.
*
* @internal $args['simple'] Optional. Hide advanced options.
* @internal $args['placeholder'] Optional. Change search placeholder.
@ -27,6 +28,12 @@ if ( $show_advanced ) {
$order = sanitize_text_field( $_GET['order'] ?? 'desc' );
$orderby = sanitize_text_field( $_GET['orderby'] ?? 'modified' );
$story_status = array_intersect(
[ $_GET['story_status'] ?? 0 ],
['Completed', 'Ongoing', 'Oneshot', 'Hiatus', 'Canceled']
);
$story_status = reset( $story_status ) ?: 0;
$all_authors = get_users(
array(
'has_published_posts' => ['fcn_story', 'fcn_chapter', 'fcn_recommendation', 'post']
@ -57,7 +64,7 @@ if ( $show_advanced ) {
$queried_ex_warnings = sanitize_text_field( $_GET['ex_warnings'] ?? 0 );
$queried_ex_tags = sanitize_text_field( $_GET['ex_tags'] ?? 0 );
$is_advanced_search = $post_type != 'any' || $sentence != '0' || $order != 'desc' || $orderby != 'modified' || $queried_tags || $queried_genres || $queried_fandoms || $queried_characters || $queried_warnings || $queried_ex_tags || $queried_ex_genres || $queried_ex_fandoms || $queried_ex_characters || $queried_ex_warnings || $queried_authors_in || $queried_authors_out || $author_name;
$is_advanced_search = $post_type != 'any' || $sentence != '0' || $order != 'desc' || $orderby != 'modified' || $queried_tags || $queried_genres || $queried_fandoms || $queried_characters || $queried_warnings || $queried_ex_tags || $queried_ex_genres || $queried_ex_fandoms || $queried_ex_characters || $queried_ex_warnings || $queried_authors_in || $queried_authors_out || $author_name || $story_status;
// Prime author cache
if ( function_exists( 'update_post_author_caches' ) ) {
@ -187,6 +194,13 @@ if ( $show_advanced ) {
);
?></span>
<span class="search-form__current-status"><?php
printf(
_x( '<b>Status:</b> <span>%s</span>', 'Advanced search summary.', 'fictioneer' ),
$story_status ? fcntr( $story_status ) : _x( 'Any', 'Advanced search option.', 'fictioneer' )
);
?></span>
<?php
// [query string, type, class suffix, summary heading]
$queried_terms = array(
@ -305,6 +319,7 @@ if ( $show_advanced ) {
</select>
</div>
<?php do_action( 'fictioneer_search_form_filters', $args ); ?>
</div>
<?php if ( count( $allow_list ) > 1 ) : ?><hr><?php endif; ?>