2017-05-09 24 views
0

在wordpress搜索中,我試圖僅搜索帖子類型「產品」。 但是它仍然顯示我的「日記帖子」。 我正在使用名爲「search-everything」的插件來允許我的搜索擴展到一些額外的帖子功能,如標籤,(我標記了「鞋」一詞,所有鞋產品甚至在產品中使用了「鞋」一詞。標題,這是一件好事WordPress的眺望,即使沒有任何搜索插件 除此之外,搜索模板是工作體面爲什麼我的「產品帖子」搜索顯示「日誌帖子」?

請看看該網站進行測試:http://jadepalacecollective.com/?s=shoes

enter image description here 例以上是搜索「騾子」 ,它也從日誌中檢索一篇文章(底部的大圖)

這是我的代碼(我已經精簡下來的可讀性的緣故) 我第一次去這個主要的search.php頁面

<?php 
    if (get_post_type() == 'product') : ?> 

     <header class="page-header"> 
      <h1 class="page-title searchresults-for"><?php printf(esc_html__('Search Results for: %s', 'palace'), '<span>' . get_search_query() . '</span>'); ?></h1> 
     </header><!-- .page-header --> 

     <?php 
     /* Start the Loop */ 
     while (have_posts()) : the_post(); 
      get_template_part('template-parts/content', 'search'); 
     endwhile; 
    else : 
     get_template_part('template-parts/content', 'none'); 
    endif; ?> 

如果發現的search.php它的東西,然後取模板搜索內容.php顯示有關產品的一些信息。

<?php if (get_post_type() == 'product') : ?> 

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 

<a href="<?php the_permalink(); ?>"> 
    <?php the_post_thumbnail(); ?> 
</a> 
</article><!-- #post-## --> 
<?php endif; ?> 

你會碰巧猜出爲什麼會發生這種情況嗎?我做錯了什麼嗎?

回答

1

您可以限制對特定文章類型與此代碼的搜索結果在您的functions.php

function search_rules($query) { 
    if ($query->is_search) { 
    $query->set('post_type', 'product'); 
    } 
    return $query; 
} 
add_filter('pre_get_posts','search_rules'); 
+0

尼斯......如此容易得多。非常感謝Alex。 –