1

I need the "wordpress search" engine to be only for the content of the posts. I looking for some solution but always "wordpress search" engine only shows results by titles of the post. What can I modify or what should I do to solve this problem? Thanks for you help!

3

Stright from Wordpress reference:

https://developer.wordpress.org/reference/hooks/posts_search/#user-contributed-notes

You can search by post title only with:

function __search_by_title_only( $search, $wp_query )
{
    global $wpdb;
    if(empty($search)) {
        return $search; // skip processing - no search term in query
    }
    $q = $wp_query->query_vars;
    $n = !empty($q['exact']) ? '' : '%';
    $search =
    $searchand = '';
    foreach ((array)$q['search_terms'] as $term) {
        $term = esc_sql($wpdb->esc_like($term));
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
    }
    if (!empty($search)) {
        $search = " AND ({$search}) ";
        if (!is_user_logged_in())
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    return $search;
}
add_filter('posts_search', '__search_by_title_only', 500, 2);

If you wanted to search by post content only, you'd change the above slightly, and do:

        $search .= "{$searchand}($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.