From 5ecbddeee1a9b81fa2fd8a42017e297474ffe80f Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Wed, 12 Aug 2020 11:06:58 -0300 Subject: [PATCH] Adds option to include Tainacan items in the default WP search results. --- src/functions.php | 18 +++++++++++++++++- src/functions/customizer.php | 17 ++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/functions.php b/src/functions.php index aa9dc9f..3a76896 100644 --- a/src/functions.php +++ b/src/functions.php @@ -152,7 +152,6 @@ if ( ! function_exists( 'tainacan_setup' ) ) { add_theme_support( 'custom-units' ); add_theme_support( 'editor-style' ); add_editor_style( 'editor-style.css' ); - } } // End if(). @@ -185,6 +184,23 @@ function tainacan_customize_editor_css() { } add_action( 'admin_head', 'tainacan_customize_editor_css'); +/** + * 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_include_items_in_search_results( $query ) { + + if ( get_theme_mod( 'tainacan_search_include_items', false ) ) { + if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) { + $collections_post_types = \Tainacan\Repositories\Repository::get_collections_db_identifiers(); + $query->set( 'post_type', array_merge( ['post'], $collections_post_types ) ); + } + } +} +add_action( 'pre_get_posts', 'tainacan_include_items_in_search_results' ); + /** * Set the content width based on the theme's design and stylesheet. * diff --git a/src/functions/customizer.php b/src/functions/customizer.php index 8d3b086..791bf28 100644 --- a/src/functions/customizer.php +++ b/src/functions/customizer.php @@ -125,7 +125,22 @@ function tainacan_customize_register( $wp_customize ) { 'type' => 'checkbox', 'settings' => 'tainacan_hide_search_input', 'section' => 'tainacan_header_settings', - 'label' => __( 'Hide search icon and input', 'tainacan-interface' ), + 'label' => __( 'Hide search icon and input', 'tainacan-interface' ) + ) ); + + // Includes tainacan itens on search results + $wp_customize->add_setting( 'tainacan_search_include_items', array( + 'type' => 'theme_mod', + 'default' => false, + 'capability' => 'edit_theme_options', + 'sanitize_callback' => 'tainacan_callback_sanitize_checkbox', + ) ); + $wp_customize->add_control( 'tainacan_search_include_items', array( + 'type' => 'checkbox', + 'settings' => 'tainacan_search_include_items', + 'section' => 'tainacan_header_settings', + 'label' => __( 'Include tainacan itens on the search results list', 'tainacan-interface' ), + 'description' => __( 'Toggle to include tainacan itens on the search results list. By default it only lists WordPress Posts and Pages. The items will be displayed with the same appearence of posts, with their title, thumbnail and description.', 'tainacan-interface' ) ) ); /**