Add preselect term params to search form shortcode

This commit is contained in:
Tetrakern 2024-05-24 14:21:50 +02:00
parent 9ec44c8748
commit 5978b395c4
3 changed files with 41 additions and 2 deletions

View File

@ -1075,6 +1075,11 @@ Renders the search form with advanced options (if not disabled in the settings).
* **simple:** Set `true` to hide the advanced search options. Default `false`.
* **placeholder:** Change the placeholder text.
* **type:** Preselect either "any", "story", "chapter", "recommendation", "collection", or "post".
* **tags:** Preselect tags as comma-separated list of term IDs.
* **genres:** Preselect genres as comma-separated list of term IDs.
* **fandoms:** Preselect fandoms as comma-separated list of term IDs.
* **characters:** Preselect characters as comma-separated list of term IDs.
* **warnings:** Preselect warnings as comma-separated list of term IDs.
```
[fictioneer_search]
@ -1084,6 +1089,10 @@ Renders the search form with advanced options (if not disabled in the settings).
[fictioneer_search simple="true" placeholder="What are you looking for?"]
```
```
[fictioneer_search tags="569" fandoms="200,199"]
```
![Contact Form](repo/assets/shortcode_example_search_1.jpg?raw=true)
### Showcase

View File

@ -25,6 +25,11 @@ if ( ! function_exists( 'fcn_keyword_search_taxonomies_input' ) ) {
$examples = array_rand( $taxonomies, min( 5, count( $taxonomies ) ) );
$examples = is_array( $examples ) ? $examples : [ $examples ];
if ( $args['preselected'] ?? 0 ) {
$query_list = $query_list ?
"{$query_list}," . implode( ',', $args['preselected'] ) : implode( ',', $args['preselected'] );
}
// Start HTML ---> ?>
<div class="keyword-input <?php if ( empty( $query_list ) ) echo '_empty'; ?>">
<?php if ( ! ( $args['no_operator'] ?? 0 ) ) : ?>
@ -46,8 +51,8 @@ if ( ! function_exists( 'fcn_keyword_search_taxonomies_input' ) ) {
data-hint="<?php _e( 'Start typing for suggestions…', 'fictioneer' ); ?>"
>
<?php
if ( ! empty( $query_list ) ) {
$nodes = explode( ',', $query_list );
if ( $query_list ) {
$nodes = array_unique( explode( ',', $query_list ) );
$x_mark = fictioneer_get_icon( 'fa-xmark' );
foreach ( $taxonomies as $term ) {

View File

@ -1290,6 +1290,11 @@ function fictioneer_shortcode_search( $attr ) {
$simple = isset( $attr['simple'] ) ? $attr['simple'] == 'true' || $attr['simple'] == '1' : false;
$placeholder = $attr['placeholder'] ?? false;
$type = $attr['type'] ?? false;
$pre_tags = fictioneer_explode_list( $attr['tags'] ?? '' );
$pre_genres = fictioneer_explode_list( $attr['genres'] ?? '' );
$pre_fandoms = fictioneer_explode_list( $attr['fandoms'] ?? '' );
$pre_characters = fictioneer_explode_list( $attr['characters'] ?? '' );
$pre_warnings = fictioneer_explode_list( $attr['warnings'] ?? '' );
// Prepare arguments
if ( $simple ) {
@ -1304,6 +1309,26 @@ function fictioneer_shortcode_search( $attr ) {
$args['preselect_type'] = in_array( $type, ['story', 'chapter', 'recommendation', 'collection'] ) ? "fcn_{$type}" : $type;
}
if ( $pre_tags ) {
$args['preselect_tags'] = array_map( 'absint', $pre_tags );
}
if ( $pre_genres ) {
$args['preselect_genres'] = array_map( 'absint', $pre_genres );
}
if ( $pre_fandoms ) {
$args['preselect_fandoms'] = array_map( 'absint', $pre_fandoms );
}
if ( $pre_characters ) {
$args['preselect_characters'] = array_map( 'absint', $pre_characters );
}
if ( $pre_warnings ) {
$args['preselect_warnings'] = array_map( 'absint', $pre_warnings );
}
// Buffer
ob_start();