Bạn hãy thêm đoạn code sau vào file functions.php

add_filter('posts_search','tmdev_custom_search_wordpress', 500, 2);
function tmdev_custom_search_wordpress( $search, $wp_query )
    {
        global $wpdb;
        if ( empty( $search ) )
            return $search;
        $q = $wp_query->query_vars;    
        $n = ! empty( $q['exact'] ) ? '' : '%';
        $n = '';

        $search = $searchand = '';
        
        $term_post_content = implode(" ", $q['search_terms']);
        $term_post_content = esc_sql( like_escape( $term_post_content ) );

        foreach ( (array) $q['search_terms'] as $key1 => $term ) {
            $term = esc_sql( like_escape( $term ) );
            if( $key1 == 0 ){
                $term = $term.'-';
            }
            if( $key1 > 0 ){
                $term = '-'.$term;
            }
            $search .= "{$searchand}($wpdb->posts.post_name REGEXP '{$n}{$term}{$n}')";
            $searchand = ' AND ';
        }    

        $search .= " OR ($wpdb->posts.post_content REGEXP '{$n}{$term_post_content}{$n}')";

        if ( ! empty( $search ) ) {
            $search = " AND ({$search}) ";
            if ( ! is_user_logged_in() )
                $search .= " AND ($wpdb->posts.post_password = '') ";
        }
        
        return $search;
}

 

Bài viết liên quan

post-no-image

Add the Meta Box Upload Multiple Images and multiple metabox

post-no-image

Add the Meta Box Repeat

post-no-image

Kỹ thuật debounce trong javascript – Trì hoãn nhập từ khóa trong ô input

post-no-image

Thêm VS Code snippets

post-no-image

Query only seach by title

post-no-image

Hướng dẫn tạo form có validate, upload file nhiều bước và xử lý ajax