仅限于文章标题,类别和标签的搜索结果

问题描述:

如果它是默认的WordPress搜索或自定义搜索表单,则仅匹配帖子标题,类别和标签中的搜索文本。仅限于文章标题,类别和标签的搜索结果

使用可以得到解决,

$args = array(
      'post_type' => 'post', 
      's' => $keyword, 
      'cat' => $catSearchID, 
      'tag' => $tag-slug, 
      'posts_per_page' => -1, 
      'location' => $post_location_search , 
     ); 

$wp_query = new WP_Query($args); 

搜索表单应该这样。 {} post_type .PHP -

<form class="search" action="<?php echo home_url('/'); ?>"> 
    <input type="search" name="s" placeholder="Search&hellip;"> 
    <input type="submit" value="Search"> 
    <input type="hidden" name="post_type" value="kb_article"> 
</form> 

我们将使用{}模板的WordPress的文件名命名惯例我们的定制搜索结果模板。在我们的例子中,这将导致search-kb_article.php。我们可以检查是否在URL字符串中设置了post_type = kb_article,如果是,则使用get_template_part加载它,但让我们更进一步。我们将检查是否在URL字符串中设置了post_type,如果是,则使用{template} - {post_type} .php命名模式检查该自定义搜索模板是否存在。如果是这样,我们会加载它。如果没有,我们将继续使用默认搜索模板。下面的查询

<?php 
// store the post type from the URL string 
$post_type = $_GET['post_type']; 
// check to see if there was a post type in the 
// URL string and if a results template for that 
// post type actually exists 
if (isset($post_type) && locate_template('search-' . $post_type . '.php')) { 
    // if so, load that template 
    get_template_part('search', $post_type); 

    // and then exit out 
    exit; 
} 
?> 
+0

号我问结果取,不覆盖默认搜索模板。 – GNANA