Add type param to search shortcode

This commit is contained in:
Tetrakern 2023-07-28 11:25:52 +02:00
parent e39ddb8bde
commit e09fb47758
2 changed files with 13 additions and 5 deletions

View File

@ -1086,20 +1086,27 @@ add_shortcode( 'fictioneer_contact_form', 'fictioneer_shortcode_contact_form' );
* *
* @param string|null $attr['simple'] Optional. Hide the advanced options. * @param string|null $attr['simple'] Optional. Hide the advanced options.
* @param string|null $attr['placeholder'] Optional. Placeholder text. * @param string|null $attr['placeholder'] Optional. Placeholder text.
* @param string|null $attr['type'] Optional. Default post type to query.
* *
* @return string The rendered shortcode HTML. * @return string The rendered shortcode HTML.
*/ */
function fictioneer_shortcode_search( $attr ) { function fictioneer_shortcode_search( $attr ) {
// Setup // Setup
$args = ['cache' => true]; $args = array( 'cache' => true );
$simple = isset( $attr['simple'] ) ? $attr['simple'] == 'true' || $attr['simple'] == '1' : false; $simple = isset( $attr['simple'] ) ? $attr['simple'] == 'true' || $attr['simple'] == '1' : false;
$placeholder = $attr['placeholder'] ?? false; $placeholder = $attr['placeholder'] ?? false;
$type = $attr['type'] ?? false;
// Prepare arguments // Prepare arguments
if ( $simple ) $args['simple'] = $simple; if ( $simple ) $args['simple'] = $simple;
if ( $placeholder ) $args['placeholder'] = $placeholder; if ( $placeholder ) $args['placeholder'] = $placeholder;
if ( $type && in_array( $type, ['any', 'story', 'chapter', 'recommendation', 'collection', 'post'] ) ) {
$args['preselect_type'] = in_array( $type, ['story', 'chapter', 'recommendation', 'collection'] ) ? "fcn_$type" : $type;
}
// Buffer // Buffer
ob_start(); ob_start();

View File

@ -6,9 +6,10 @@
* @subpackage Fictioneer * @subpackage Fictioneer
* @since 5.0 * @since 5.0
* *
* @internal $args['simple'] Optional. Hide advanced options. * @internal $args['simple'] Optional. Hide advanced options.
* @internal $args['placeholder'] Optional. Change search placeholder. * @internal $args['placeholder'] Optional. Change search placeholder.
* @internal $args['cache'] Whether to account for active caching. * @internal $args['preselect_type'] Optional. Default post type to query.
* @internal $args['cache'] Whether to account for active caching.
*/ */
?> ?>
@ -20,7 +21,7 @@ $cache_mode = isset( $args['cache'] ) && $args['cache'];
$show_advanced = ! get_option( 'fictioneer_disable_theme_search' ) && ! $simple_mode; $show_advanced = ! get_option( 'fictioneer_disable_theme_search' ) && ! $simple_mode;
$placeholder = $args['placeholder'] ?? _x( 'Search keywords or phrase', 'Advanced search placeholder.', 'fictioneer' ); $placeholder = $args['placeholder'] ?? _x( 'Search keywords or phrase', 'Advanced search placeholder.', 'fictioneer' );
$toggle_id = wp_unique_id( 'fictioneer-advanced-search-toggle-' ); $toggle_id = wp_unique_id( 'fictioneer-advanced-search-toggle-' );
$post_type = $_GET['post_type'] ?? 'any'; $post_type = $_GET['post_type'] ?? $args['preselect_type'] ?? 'any';
$sentence = $_GET['sentence'] ?? '0'; $sentence = $_GET['sentence'] ?? '0';
$order = $_GET['order'] ?? 'desc'; $order = $_GET['order'] ?? 'desc';
$orderby = $_GET['orderby'] ?? 'modified'; $orderby = $_GET['orderby'] ?? 'modified';