From 0fab47b0eea97cf12504ac6376c73f02badf3dcd Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Tue, 30 Nov 2021 16:29:48 -0300 Subject: [PATCH] Fixes template redirect from search bar. --- tainacan-blocksy/inc/plugin.php | 73 ++++++++++++++------------------- 1 file changed, 30 insertions(+), 43 deletions(-) diff --git a/tainacan-blocksy/inc/plugin.php b/tainacan-blocksy/inc/plugin.php index 28f4166..23f2792 100644 --- a/tainacan-blocksy/inc/plugin.php +++ b/tainacan-blocksy/inc/plugin.php @@ -7,17 +7,42 @@ if ( !function_exists('tainacan_blocksy_archive_templates_redirects') ) { function tainacan_blocksy_archive_templates_redirects() { global $wp_query; + + if ( + isset( $_GET['s'] ) && + $wp_query->is_main_query() && + $wp_query->is_search() && + !is_admin() + ) { + $collections_post_types = \Tainacan\Repositories\Repository::get_collections_db_identifiers(); + $searching_post_types = $wp_query->get( 'post_type' ); + + if ( !is_array($searching_post_types) ) + $searching_post_types = [ $searching_post_types ]; + + // If the search is going on post types other than Tainacan items... + foreach($searching_post_types as $searching_post_type) { + if ( !in_array($searching_post_type, $collections_post_types) ) + return; + } + + // If the search is in a single collection, go there + if ( count($searching_post_types) === 1 ) + wp_redirect( get_post_type_archive_link( $searching_post_types[0] ) . '?search=' . $_GET['s'] ); + + // Otherwise, the Items Repository list should do the job + else + wp_redirect( \Tainacan\Theme_Helper::get_instance()->get_items_list_slug() . '?search=' . $_GET['s'] ); + } + if (is_post_type_archive()) { $collections_post_types = \Tainacan\Repositories\Repository::get_collections_db_identifiers(); $current_post_type = get_post_type(); if (in_array($current_post_type, $collections_post_types)) { - - if (is_post_type_archive()) { - include( TAINACAN_BLOCKSY_PLUGIN_DIR_PATH . '/tainacan/archive-items.php' ); - exit; - } + include( TAINACAN_BLOCKSY_PLUGIN_DIR_PATH . '/tainacan/archive-items.php' ); + exit; } } else if ( is_tax() ) { $term = get_queried_object(); @@ -102,43 +127,5 @@ function tainacan_get_default_view_mode_choices() { ]; } -/** - * This function modifies the main WordPress query to include an array of - * post types instead of the default 'post' post type. - * - * @param object $query The main WordPress query. - */ -function tainacan_blocksy_modify_search_query( ) { - global $wp_query; - - if ( - !is_post_type_archive() && - isset( $_GET['s'] ) && - $wp_query->is_main_query() && - $wp_query->is_search() && - !is_admin() - ) { - $collections_post_types = \Tainacan\Repositories\Repository::get_collections_db_identifiers(); - $searching_post_types = $wp_query->get( 'post_type' ); - - if ( !is_array($searching_post_types) ) - $searching_post_types = [ $searching_post_types ]; - - // If the search is going on post types other than Tainacan items... - foreach($searching_post_types as $searching_post_type) { - if ( !in_array($searching_post_type, $collections_post_types) ) - return; - } - - // If the search is in a single collection, go there - if ( count($searching_post_types) === 1 ) - wp_redirect( get_post_type_archive_link( $searching_post_types[0] ) . '?search=' . $_GET['s'] ); - - // Otherwise, the Items Repository list should do the job - else - wp_redirect( \Tainacan\Theme_Helper::get_instance()->get_items_list_slug() . '?search=' . $_GET['s'] ); - } -} -add_action( 'template_redirect', 'tainacan_blocksy_modify_search_query' ); ?> \ No newline at end of file