Add word limits to search form
This commit is contained in:
parent
c3bc1980ea
commit
ce3880c33e
@ -205,6 +205,8 @@ function fictioneer_extend_search_query( $query ) {
|
||||
// Setup
|
||||
$tax_array = [];
|
||||
$authors = [];
|
||||
$min_words = absint( $_GET['min_words'] ?? 0 );
|
||||
$max_words = absint( $_GET['max_words'] ?? 0 );
|
||||
|
||||
$is_any_post = isset( $_GET['post_type'] ) && ( $_GET['post_type'] === 'any' ) ? 1 : 0;
|
||||
$authors_in = empty( $_GET['authors'] ) ? [] : array_map( 'absint', explode( ',', $_GET['authors'] ) );
|
||||
@ -427,6 +429,48 @@ function fictioneer_extend_search_query( $query ) {
|
||||
);
|
||||
}
|
||||
|
||||
if ( $min_words ) {
|
||||
$meta_query_stack[] = array(
|
||||
'relation' => 'OR',
|
||||
array(
|
||||
'key' => '_word_count',
|
||||
'value' => $min_words,
|
||||
'compare' => '>=',
|
||||
'type' => 'numeric'
|
||||
),
|
||||
array(
|
||||
'key' => 'fictioneer_story_total_word_count',
|
||||
'value' => $min_words,
|
||||
'compare' => '>=',
|
||||
'type' => 'numeric'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( $max_words ) {
|
||||
$meta_query_stack[] = array(
|
||||
'relation' => 'OR',
|
||||
array(
|
||||
'key' => '_word_count',
|
||||
'value' => $max_words,
|
||||
'compare' => '<=',
|
||||
'type' => 'numeric'
|
||||
),
|
||||
array(
|
||||
'key' => 'fictioneer_story_total_word_count',
|
||||
'value' => $max_words,
|
||||
'compare' => '<=',
|
||||
'type' => 'numeric'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Only query stories and chapters for word limits
|
||||
if ( $min_words || $max_words ) {
|
||||
$types = (array) $query->get( 'post_type' );
|
||||
$query->set( 'post_type', array_intersect( $types, ['fcn_story', 'fcn_chapter'] ) );
|
||||
}
|
||||
|
||||
if ( $meta_query_stack ) {
|
||||
$meta_query = [];
|
||||
|
||||
|
@ -749,6 +749,68 @@ function fictioneer_add_search_for_status( $args ) {
|
||||
}
|
||||
add_action( 'fictioneer_search_form_filters', 'fictioneer_add_search_for_status' );
|
||||
|
||||
/**
|
||||
* Adds min words select to advanced search form
|
||||
*
|
||||
* @since 5.13.1
|
||||
*
|
||||
* @param array $args Arguments passed to the search form.
|
||||
*/
|
||||
|
||||
function fictioneer_add_search_for_min_words( $args ) {
|
||||
$words = absint( $_GET['min_words'] ?? 0 );
|
||||
|
||||
// Start HTML ---> ?>
|
||||
<div class="search-form__select-wrapper select-wrapper">
|
||||
<div class="search-form__select-title"><?php _ex( 'Min Words', 'Advanced search heading.', 'fictioneer' ); ?></div>
|
||||
<select name="min_words" class="search-form__select" autocomplete="off" data-default="0">
|
||||
<option value="0" <?php echo ! $words ? 'selected' : ''; ?>><?php _ex( 'Minimum', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="1000" <?php echo $words == 1000 ? 'selected' : ''; ?>><?php _ex( '1,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="5000" <?php echo $words == 5000 ? 'selected' : ''; ?>><?php _ex( '5,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="10000" <?php echo $words == 10000 ? 'selected' : ''; ?>><?php _ex( '10,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="25000" <?php echo $words == 25000 ? 'selected' : ''; ?>><?php _ex( '25,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="50000" <?php echo $words == 50000 ? 'selected' : ''; ?>><?php _ex( '50,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="100000" <?php echo $words == 100000 ? 'selected' : ''; ?>><?php _ex( '100,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="250000" <?php echo $words == 250000 ? 'selected' : ''; ?>><?php _ex( '250,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="500000" <?php echo $words == 500000 ? 'selected' : ''; ?>><?php _ex( '500,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="1000000" <?php echo $words == 1000000 ? 'selected' : ''; ?>><?php _ex( '1,000,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
</select>
|
||||
</div>
|
||||
<?php // <--- End HTML
|
||||
}
|
||||
add_action( 'fictioneer_search_form_filters', 'fictioneer_add_search_for_min_words' );
|
||||
|
||||
/**
|
||||
* Adds max words select to advanced search form
|
||||
*
|
||||
* @since 5.13.1
|
||||
*
|
||||
* @param array $args Arguments passed to the search form.
|
||||
*/
|
||||
|
||||
function fictioneer_add_search_for_max_words( $args ) {
|
||||
$words = absint( $_GET['max_words'] ?? 0 );
|
||||
|
||||
// Start HTML ---> ?>
|
||||
<div class="search-form__select-wrapper select-wrapper">
|
||||
<div class="search-form__select-title"><?php _ex( 'Max Words', 'Advanced search heading.', 'fictioneer' ); ?></div>
|
||||
<select name="max_words" class="search-form__select" autocomplete="off" data-default="0">
|
||||
<option value="0" <?php echo ! $words ? 'selected' : ''; ?>><?php _ex( 'Maximum', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="1000" <?php echo $words == 1000 ? 'selected' : ''; ?>><?php _ex( '1,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="5000" <?php echo $words == 5000 ? 'selected' : ''; ?>><?php _ex( '5,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="10000" <?php echo $words == 10000 ? 'selected' : ''; ?>><?php _ex( '10,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="25000" <?php echo $words == 25000 ? 'selected' : ''; ?>><?php _ex( '25,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="50000" <?php echo $words == 50000 ? 'selected' : ''; ?>><?php _ex( '50,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="100000" <?php echo $words == 100000 ? 'selected' : ''; ?>><?php _ex( '100,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="250000" <?php echo $words == 250000 ? 'selected' : ''; ?>><?php _ex( '250,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="500000" <?php echo $words == 500000 ? 'selected' : ''; ?>><?php _ex( '500,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
<option value="1000000" <?php echo $words == 1000000 ? 'selected' : ''; ?>><?php _ex( '1,000,000 Words', 'Advanced search option.', 'fictioneer' ); ?></option>
|
||||
</select>
|
||||
</div>
|
||||
<?php // <--- End HTML
|
||||
}
|
||||
add_action( 'fictioneer_search_form_filters', 'fictioneer_add_search_for_max_words' );
|
||||
|
||||
/**
|
||||
* Outputs the HTML for no search params
|
||||
*
|
||||
|
@ -27,6 +27,8 @@ if ( $show_advanced ) {
|
||||
$sentence = sanitize_text_field( $_GET['sentence'] ?? 0 );
|
||||
$order = sanitize_text_field( $_GET['order'] ?? 'desc' );
|
||||
$orderby = sanitize_text_field( $_GET['orderby'] ?? 'modified' );
|
||||
$min_words = absint( $_GET['min_words'] ?? 0 );
|
||||
$max_words = absint( $_GET['max_words'] ?? 0 );
|
||||
|
||||
$story_status = array_intersect(
|
||||
[ $_GET['story_status'] ?? 0 ],
|
||||
@ -70,7 +72,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 || $story_status || $age_rating;
|
||||
$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 || $age_rating || $min_words || $max_words;
|
||||
|
||||
// Prime author cache
|
||||
if ( function_exists( 'update_post_author_caches' ) ) {
|
||||
@ -214,6 +216,24 @@ if ( $show_advanced ) {
|
||||
);
|
||||
?></span>
|
||||
|
||||
<?php if ( $min_words ) : ?>
|
||||
<span class="search-form__current-status"><?php
|
||||
printf(
|
||||
_x( '<b>Min Words:</b> <span>%s</span>', 'Advanced search summary.', 'fictioneer' ),
|
||||
$min_words ? $min_words : _x( 'Any', 'Advanced search option.', 'fictioneer' )
|
||||
);
|
||||
?></span>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ( $max_words ) : ?>
|
||||
<span class="search-form__current-status"><?php
|
||||
printf(
|
||||
_x( '<b>Max Words:</b> <span>%s</span>', 'Advanced search summary.', 'fictioneer' ),
|
||||
$max_words ? $max_words : _x( 'Any', 'Advanced search option.', 'fictioneer' )
|
||||
);
|
||||
?></span>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php
|
||||
// [query string, type, class suffix, summary heading]
|
||||
$queried_terms = array(
|
||||
|
Loading…
x
Reference in New Issue
Block a user