为什么我的“产品帖子”搜索显示“日志帖子”?

问题描述:

在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; ?> 

你会碰巧猜出为什么会发生这种情况吗?我做错了什么吗?

您可以限制对特定文章类型与此代码的搜索结果在您的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。 –