I want the result of the Wordpess search on the homepage to refer exclusively to the post title and the post text and I have written a plugin for this. However, articles also appear in the search results where the search term can only be found in the GUID of the article.
`<?php
/*
Plugin Name: Custom Search Plugin
Description: Passt die Suchergebnisse so an, dass nur in den Überschriften und im Text von Artikeln gesucht wird.
Version: 1.0
Author: Michael Barth
*/
if ( ! defined( ‘ABSPATH’ ) ) {
exit; // Verhindert direkten Zugriff
}
function custom_search_where( $where, $wp_query ) {
global $wpdb;
if ( $wp_query->is_search && !is_admin() ) {
// Bereinigen der Sucheingabe
$search_terms = get_query_var( 's' );
$search_terms = $wpdb->esc_like( $search_terms );
$search_terms = '%' . $search_terms . '%';
// Anpassung der WHERE-Klausel, sodass nur in Titel und Inhalt gesucht wird
$where = "
AND (
({$wpdb->posts}.post_title LIKE '{$search_terms}')
OR
({$wpdb->posts}.post_content LIKE '{$search_terms}')
)
AND ({$wpdb->posts}.post_type = 'post' AND {$wpdb->posts}.post_status = 'publish')
";
}
return $where;
}
add_filter( ‘posts_search’, ‘custom_search_where’, 10, 2 );
`
WordPress is installed on a domain barth.education. When I search for “Barth”, all articles are displayed, even though I have limited the search to titles and text using a plugin. How can I prevent this? I have deactivated all plugins except Elementor, Elementor pro and my custom_search_plugin and don’t know any advice.
Many thanks for the help!