douqi3195 2016-08-09 11:54
浏览 29
已采纳

Wordpress搜索不能使用超过87个字符的搜索字词

I have following Wordpress search setup :

search_form.php

 <input type="text" class="search_form clearable" name="s" id="s" autocomplete="off" placeholder="Search Text" />

search.php

 <?php if ( have_posts() && strlen( trim(get_search_query()) ) != 0 ) :
        while ( have_posts() ) :
        set_query_var( 'boxclass', 'full horizontal');
               the_post();
              .
              .
              .
        // If no content, include the "No posts found" template.
        else :
           echo '<div>No search results found</div>';
        endif;
   ?>

Now,

When I search a text of 87 characters, say :

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat"

A post result is found.

When I search text string of 88 characters which also exist in the post, say :
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequata"

It returns : No search results found

How to resolve this? Is there some limit on Wordpress search term length?

  • 写回答

2条回答 默认 最新

  • doumei1955 2016-08-10 13:41
    关注

    I found the issue to the problem. The issue lies in WP Core file where the actual search takes place, i.e.

    wp-includes/query.php => function parse_search(){.....}

    Issue was under :

    if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 ){                    
                         $q['search_terms'] = array( $q['s'] );
                        }
    

    i.e. As per WP Core method, if search string has all words in it i.e. the search terms' (excluding if/the/of etc type words filtered under function parse_search_terms() in query.php) count :

    • 1-9, the search string is split to individual words and all the search terms are individually searched(using LIKE clause) in the posts table title and content and these are ANDed together

    For ex :

     ((test_posts.post_title LIKE '%Officers%') OR (test_posts.post_content LIKE '%Officers%')) AND ((test_posts.post_title LIKE '%Manager%') OR (test_posts.post_content LIKE '%Manager%'))
    
    • 0 or >9, the search string is search as a single complete string in the DB

    For ex.

    ((test_posts.post_title LIKE '%Officers and Managers are working and coordinating into an atmosphere of healthy development in the year 1982 of bright age%') OR (test_posts.post_content LIKE '%Officers and Managers are working and coordinating into an atmosphere of healthy development in the year 1982 of bright age%'))
    

    Now, in my case, * In 86 characters, my search terms array count was 9, and thus each search term was being searched in SQL DB separately and all the searches were "AND"ed together. * In 87 characters, my search terms array count was 10, thus the final search term was NOT being split to individual words, and being searched as a single string. And since the string in the DB had an html tag in between, thus as a single string it did not give post as result.

    Thus it was not the count of characters, but the count of words in $q['search_terms'] that was posing the logic difference.

    Hope it helps.Thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效