__('Suchfeld mit Optionen für Titel- und Gastautoren-Suche', 'wp-multi-search')) ); } public function widget($args, $instance) { echo $args['before_widget']; echo '

' . __('Suche', 'wp-multi-search') . '

'; echo csp_search_form(); echo $args['after_widget']; } } function csp_register_widget() { register_widget('CSP_Search_Widget'); } add_action('widgets_init', 'csp_register_widget'); // 4️⃣ **Einstellungsseite hinzufügen** function csp_add_menu_page() { add_options_page( 'WP-Multi-Search Settings', 'WP-Multi-Search', 'manage_options', 'custom-search-settings', 'csp_render_settings_page' ); } add_action('admin_menu', 'csp_add_menu_page'); // 5️⃣ **Einstellungsseite rendern** function csp_render_settings_page() { ?>

__('Top of ShiftNav', 'wp-multi-search'), 'bottom' => __('Bottom of ShiftNav', 'wp-multi-search'), 'none' => __('Do not display in ShiftNav', 'wp-multi-search') ); $selected_position = get_option('csp_shiftnav_position', 'none'); echo ''; } // 9️⃣ **Funktion zum Einfügen des Suchformulars in ShiftNav** function csp_insert_search_form_in_shiftnav($nav_menu, $args) { if ($args->theme_location !== 'shiftnav') { return $nav_menu; } $position = get_option('csp_shiftnav_position', 'none'); if ($position === 'none') { return $nav_menu; } $search_form = ''; if ($position === 'top') { return $search_form . $nav_menu; } elseif ($position === 'bottom') { return $nav_menu . $search_form; } return $nav_menu; } add_filter('wp_nav_menu_items', 'csp_insert_search_form_in_shiftnav', 10, 2); // 🔟 **Aktualisierte Suchfunktion mit Suchtyp-Unterscheidung** function csp_custom_search_results($query) { if (!is_admin() && $query->is_search() && $query->is_main_query()) { $search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all'; $search_term = $query->get('s'); switch($search_type) { case 'title': $query->set('post_title_like', $search_term); $query->set('s', ''); add_filter('posts_where', 'csp_title_search_where', 10, 2); break; case 'guest_author': $meta_query = array( array( 'key' => '_guest_author', 'value' => $search_term, 'compare' => '=' ) ); $query->set('meta_query', $meta_query); $query->set('s', ''); $query->set('post_type', 'post'); break; default: // 'all' $query->set('_meta_or_title', $search_term); add_filter('posts_search', 'csp_custom_search_where', 10, 2); add_filter('posts_join', 'csp_custom_search_join', 10, 2); add_filter('posts_groupby', 'csp_custom_search_groupby', 10, 2); break; } // Filter nach der Abfrage entfernen add_action('wp', function() use ($search_type) { switch($search_type) { case 'title': remove_filter('posts_where', 'csp_title_search_where', 10); break; case 'guest_author': // Keine zusätzlichen Filter zum Entfernen break; default: remove_filter('posts_search', 'csp_custom_search_where', 10); remove_filter('posts_join', 'csp_custom_search_join', 10); remove_filter('posts_groupby', 'csp_custom_search_groupby', 10); break; } }); } } add_action('pre_get_posts', 'csp_custom_search_results'); // 1️⃣1️⃣ **Hilfsfunktionen für verschiedene Suchtypen** function csp_title_search_where($where, $query) { global $wpdb; if ($search_term = $query->get('post_title_like')) { $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title LIKE %s", '%' . $wpdb->esc_like($search_term) . '%' ); } return $where; } function csp_custom_search_where($where, $query) { global $wpdb; if ($search_term = $query->get('_meta_or_title')) { $search = '%' . $wpdb->esc_like($search_term) . '%'; $where .= $wpdb->prepare( " OR ({$wpdb->posts}.post_title LIKE %s) OR ({$wpdb->posts}.post_content LIKE %s) OR ({$wpdb->posts}.post_excerpt LIKE %s) OR (guest_author.meta_value LIKE %s) OR ({$wpdb->users}.display_name LIKE %s)", $search, $search, $search, $search, $search ); } return $where; } function csp_custom_search_join($join, $query) { global $wpdb; if ($query->get('_meta_or_title')) { $join .= " LEFT JOIN {$wpdb->postmeta} guest_author ON ({$wpdb->posts}.ID = guest_author.post_id AND guest_author.meta_key = '_guest_author')"; $join .= " LEFT JOIN {$wpdb->users} ON ({$wpdb->posts}.post_author = {$wpdb->users}.ID)"; } return $join; } function csp_custom_search_groupby($groupby, $query) { global $wpdb; if ($query->get('_meta_or_title')) { $groupby = "{$wpdb->posts}.ID"; } return $groupby; } // 1️⃣2️⃣ **Hilfsfunktion für Gastautoren-Suche** function csp_guest_author_search_where($where, $query) { global $wpdb; if ($query->get('meta_query')) { $where .= " AND EXISTS ( SELECT 1 FROM {$wpdb->postmeta} WHERE {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID AND {$wpdb->postmeta}.meta_key = '_guest_author' AND {$wpdb->postmeta}.meta_value != '' )"; } return $where; } add_filter('posts_where', 'csp_guest_author_search_where', 10, 2); ?>