Wordpress search engine not working properly

Viewed 66

I would like the search engine to work better. eg Now when I type the phrase Pion studio I get entries that do not have this phrase in the text or in the title.

There are my search results: https://www.designalive.pl/?s=pion+studio

I tried some solutions from this thread: https://wordpress.stackexchange.com/questions/177183/make-a-wp-query-search-match-exactly-the-search-term But it's not working for me.

EDIT1 When I type phrases in "" for example "Pion studio" the results seems to be good.
EDIT2 When I type two or more words it searches for posts with one OR second word (It should be AND).

Search function in functions.php

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( 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;
}

add_filter( 'posts_search', '__search_by_title_only', 1000, 2 );

My search.php file

<?php 
    global $post;
    $post_slug=$post->post_name;

    $TagNumber = $_GET['s'];

    global $query_string;
    $query_args = explode("&", $query_string);
    $search_query = array(
        'orderby' => 'date',
        'order'   => 'DESC',
        'tax_query' => array( array(
                            'tag' => $TagNumber
                        ))
    );

    foreach($query_args as $key => $string) {
      $query_split = explode("=", $string);
      $search_query[$query_split[0]] = urldecode($query_split[1]);
    } // foreach

    $the_query = new WP_Query($search_query);


?>
<div id="<?php echo $post_slug; ?>" class="hfeed site ">
    <div class="container">
        <div class="row">
            <div class="col-auto">
                <h1><?php single_term_title() ?></h1>
            </div>
            <div class="col d-flex align-items-center">
                <hr class="w-100">
            </div>
        </div>
    </div>
    <div class="container-fluid">
        <div class="row">
            <main class="col-12 col-lg-7 offset-lg-1">
                <div class="card-deck mb-50px search-item">
                <?php if ( $the_query->have_posts() ) : ?>
                    <?php $post_wrap = 1; ?>
                    <?php /* Start the Loop */ ?>
                    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                        <?php
                            /* Include the Post-Format-specific template for the content.
                             * If you want to override this in a child theme, then include a file
                             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                             */
                            get_template_part( 'content', get_post_format() );
                        ?>
                        <?php
                        if ($post_wrap % 2 == 1){
                            echo '<div class="w-100 d-none d-sm-block"><!-- wrap every 2 on sm+--></div>';
                        }
                        
                        
                        $post_wrap++; ?>
                    <?php endwhile; ?>
                    
                    
                    
                <?php else : ?>

                    <?php get_template_part( 'content', 'none' ); ?>

                <?php endif; ?>
            </div>
            <?php hubdabwpt_paging_nav(); ?>
            
            </main><!-- #main -->
            <?php get_sidebar(); ?> 
        </div>
    </div>
</div>


<?php get_footer(); ?>
0 Answers
Related