Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter diver214

    (@diver214)

    Hello,

    I have found a way to search specific words and not partial words in my main search bar on my template, but when I use this code (in my theme’s function.php) your search tool doesn’t work. Can I incorporate your code to work with the following?

    add_filter( ‘posts_search’, ‘my_search_is_perfect’, 20, 2 );
    function my_search_is_perfect( $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( like_escape( $term ) );

    $search .= “{$searchand}($wpdb->posts.post_title REGEXP ‘[[:<:]]{$term}[[:>:]]’) OR ($wpdb->posts.post_content REGEXP ‘[[:<:]]{$term}[[:>:]]’)”;

    $searchand = ‘ AND ‘;
    }

    if ( ! empty( $search ) ) {
    $search = ” AND ({$search}) “;
    if ( ! is_user_logged_in() )
    $search .= ” AND ($wpdb->posts.post_password = ”) “;
    }

    return $search;
    }

Viewing 1 replies (of 1 total)