• Resolved fkoomek

    (@fkoomek)


    Hello.

    Is it possible to force WordPress to respect diacritics when searching? For example when I have a word “měna”, I would like to return only words containing “měna”, not “mena”.
    Thank you .

    Radan

Viewing 4 replies - 1 through 4 (of 4 total)
  • I have not tested yet your case, but you could try this plugin: https://www.ads-software.com/plugins/relevanssi/

    Thread Starter fkoomek

    (@fkoomek)

    Hello.

    Thank you.
    Isn’t there really any easier way to change this WordPress behavior? I am creating a simple dictionary, I finished my work and the only last thing I noticed is just this issue. I read something about collate and tried some hooks but nothing works.

    Many thanks.

    Radan

    Thread Starter fkoomek

    (@fkoomek)

    Here is the working code. For anybody else, I guess you should change ‘utf8mb4_czech_ci’ according to your language.

    function wpdocs_custom_posts_search( $search, $wp_query ) {
        global $wpdb;
     
        if ( empty( $search ) )
            return $search;
     
        $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 ) );
            $like = $n . $term . $n;
            $search .= "{$searchand}CONVERT({$wpdb->posts}.post_title USING utf8mb4) COLLATE utf8mb4_czech_ci LIKE '{$like}'";
            $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', 'wpdocs_custom_posts_search', 500, 2 );

    Thread Starter fkoomek

    (@fkoomek)

    Resolved.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Force WordPress search respect diacritics’ is closed to new replies.