Fixes template redirect from search bar.

This commit is contained in:
mateuswetah 2021-11-30 16:29:48 -03:00
parent 8c80ae5044
commit 0fab47b0ee
1 changed files with 30 additions and 43 deletions

View File

@ -7,18 +7,43 @@
if ( !function_exists('tainacan_blocksy_archive_templates_redirects') ) { if ( !function_exists('tainacan_blocksy_archive_templates_redirects') ) {
function tainacan_blocksy_archive_templates_redirects() { function tainacan_blocksy_archive_templates_redirects() {
global $wp_query; 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()) { if (is_post_type_archive()) {
$collections_post_types = \Tainacan\Repositories\Repository::get_collections_db_identifiers(); $collections_post_types = \Tainacan\Repositories\Repository::get_collections_db_identifiers();
$current_post_type = get_post_type(); $current_post_type = get_post_type();
if (in_array($current_post_type, $collections_post_types)) { if (in_array($current_post_type, $collections_post_types)) {
if (is_post_type_archive()) {
include( TAINACAN_BLOCKSY_PLUGIN_DIR_PATH . '/tainacan/archive-items.php' ); include( TAINACAN_BLOCKSY_PLUGIN_DIR_PATH . '/tainacan/archive-items.php' );
exit; exit;
} }
}
} else if ( is_tax() ) { } else if ( is_tax() ) {
$term = get_queried_object(); $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' );
?> ?>