2016-06-16 25 views
0

我在我的一個wordpress網站中使用平面主題。在我編寫多個單詞(如「問題跟蹤器」)時,此搜索不起作用。對於一個詞它工作正常,像「問題」或「跟蹤」。 searchform.php文件的代碼是:WordPress的搜索不能用多個單詞

<form method="get" id="searchform" action="<?php echo esc_url(home_url('/')); ?>"> 
     <label for="s" class="assistive-text"><?php _e('Search', 'flat'); ?></label> 
     <input type="text" class="field" name="s" id="s" placeholder="<?php esc_attr_e('Search', 'flat'); ?>" /> 
     <input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e('Search', 'flat'); ?>" /> 
    </form> 

和我的search.php文件是:

<?php get_header(); ?> 

<?php flat_hook_search_before(); ?> 
    <h1 class="page-title"><?php printf(__('Search Results for: %s', 'flat'), get_search_query()); ?></h1> 
    <div id="content" class="site-content" role="main"> 
     <?php flat_hook_search_top(); ?> 

    <?php if (have_posts()) : ?> 

     <?php while (have_posts()) : the_post(); ?> 
      <?php get_template_part('content', get_post_format()); ?> 
     <?php endwhile; ?> 
     <?php the_posts_pagination(array('prev_text' => __('<i class="fa fa-chevron-left"></i>', 'flat'), 'next_text' => __('<i class="fa fa-chevron-right"></i>', 'flat'))); ?> 
    <?php else : ?> 
     <?php get_template_part('content', 'none'); ?> 
    <?php endif; ?> 

     <?php flat_hook_search_bottom(); ?> 
    </div> 
    <?php flat_hook_search_after(); ?> 
<?php get_footer(); ?> 

以前它工作在我的本地設置,但現在它不工作對我的真人版。

請提出解決方案。

回答

0

我已經嘗試了很多來糾正這個問題,但無法弄清楚。爲了糾正我的搜索,我使用了自定義的SQL查詢並且工作正常。

因爲我需要一個簡單的wordpress搜索帖子標題和內容,應該適用於多個單詞。我的代碼如下:

把這個functions.php中:

function custom_sql_for_search() { 

    global $wpdb; 

    $search_string = esc_sql($_GET['s']); 


    $sql = "SELECT * FROM {$wpdb->base_prefix}posts WHERE 

       (post_title LIKE '%".$search_string."%' OR post_content LIKE '%".$search_string."%') 
       AND (post_status = 'publish') 
       ORDER BY post_type DESC, post_date DESC"; 

       $query=$wpdb->get_results($sql); 

       foreach ($query as $post) { 
         $result[$post->ID] = $post->ID; 
       } 

      return $result; 
} 

現在我修改的search.php代碼如下:

<?php get_header(); 

flat_hook_search_before(); 

$search_result = custom_sql_for_search(); 

     ?> 

      <h1 class="page-title"><?php printf(__('Search Results for: %s', 'flat'), get_search_query()); ?></h1> 

     <div id="content" class="site-content" role="main"> 

     <?php flat_hook_search_top(); 

      if (count($search_result) > 0) { 
      foreach($search_result as $post) { 

       $post = get_post($post->ID); 
       setup_postdata($post); 

       get_template_part('content', get_post_format()); 

      } 


     } else { 

      get_template_part('content', 'none'); 
     } 

     flat_hook_search_bottom(); 

    ?> 
     </div> 

<?php flat_hook_search_after(); 
     get_footer(); ?> 

我的搜索工作後,多個單詞這個。但我在這裏面臨另一個問題。當我搜索有特殊字符的東西時,它不起作用。它不適用於引號,撇號等。

如果有人能糾正這種情況,請大家幫忙。