【问题标题】:Advanced search using a text input to search through only one category使用文本输入的高级搜索仅搜索一个类别
【发布时间】:2018-11-02 14:41:42
【问题描述】:

这样做的目的是尽可能多地使用 WordPress 的核心功能。

advanced.php - 这是自定义搜索表单...

<form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">

<input type="hidden" name="search" value="post">

<input id="search-case-study" class="search-case-study" type="text" value="" placeholder="Search..." name="s" />

<input type="submit" id="searchsubmit" value="Search" />

functions.php

// CASE STUDY SEARCH
function advanced_search_query($query) {

    if($query->is_search()) {
        // category terms search.
        $query->set('tax_query', array(
            array(
                'taxonomy' => 'case-study',
                'field' => 'slug'
            )
        ));
    }
    return $query;
}

add_action('pre_get_posts', 'advanced_search_query');
// END CASE STUDY SEARCH

我还使用以下方法将表单调用到页面中:

<?php get_template_part( 'advanced', 'searchform' ); ?>

表单正确拉入页面。

表单包含我希望使用的字段。

我只需要帮助在 functions.php 中创建查询。

就我而言,我希望搜索的类别的 slug 是“案例研究”,它需要搜索该类别博客文章中的所有内容。返回链接、图片、标题。

【问题讨论】:

  • 这个问题可能有点不清楚:您是说只希望搜索属于一个类别case-study?这是目标吗?
  • @cale_b - 没错,谢谢!
  • @cale_b - 也感谢您的编辑

标签: php wordpress search


【解决方案1】:

税务查询并不像那样工作。您想要的结果实际上应该更简单一些。

查看您的代码,修改后(用 cmets 解释发生了什么):

function advanced_search_query( $query ) {
    if( $query->is_search() ) {
        // find the category by slug
        $term = get_category_by_slug( 'case-study' ); 
        // get the category ID
        $id = $term->term_id;
        // set the query argument to search within the category
        $query->set( 'cat', $cat_id );
    }

    // removed "return $query" - $query is passed by reference, so no need to return
}

add_action('pre_get_posts', 'advanced_search_query');

注意:这将导致所有搜索仅限于该类别。由于我猜这不是您想要的,我可以建议对您的表单以及功能进行以下修改,因此它仅在搜索源自您的表单时限制搜索:

<form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <input type="hidden" name="search" value="post">
    <input id="search-case-study" class="search-case-study" type="text" value="" placeholder="Search..." name="s" />
    <!-- add a hidden input that passes the desired category slug -->
    <input name="cat_slug" value="case-study" />
    <input type="submit" id="searchsubmit" value="Search" />
</form>

然后,更新functions.php函数如下:

function advanced_search_query( $query ) {
    // check if search AND if "cat_slug" input was present
    if( $query->is_search() && ! empty( $_GET['cat_slug'] ) ) {
        // find the category by the slug passed in the input
        $term = get_category_by_slug( $_GET['cat_slug'] ); 
        // defensive check, in case the category could not be found
        if ( ! empty( $term->term_id ) ) {
            // get the category ID
            $cat_id = $term->term_id;
            // set the query argument to search within the category
            $query->set( 'cat', $cat_id );
        }
    }

    // removed "return $query" - $query is passed by reference, so no need to return
}

add_action('pre_get_posts', 'advanced_search_query');

【讨论】:

  • 我一直在测试这段代码。我可以清楚地看到它是如何工作的,但是结果并没有像我们计划的那样缩小范围。搜索仍然贯穿产品页面。如果我将表单上的操作更改为 home_url('/blog/') 它会过滤到博客文章,但仍然不是指定的类别。除了标题之外,我也无法搜索与案例研究相关的任何内容。
  • 再次感谢您回复我的问题。 object(WP_Term)#13036 (16) { ["term_id"]=&gt; int(211) ["name"]=&gt; string(10) "Case Study" ["slug"]=&gt; string(10) "case-study" ["term_group"]=&gt; int(0) ["term_taxonomy_id"]=&gt; int(211) ["taxonomy"]=&gt; string(8) "category" ["description"]=&gt; string(0) "" ["parent"]=&gt; int(0) ["count"]=&gt; int(2) ["filter"]=&gt; string(3) "raw" ["cat_ID"]=&gt; int(211) ["category_count"]=&gt; int(2) ["category_description"]=&gt; string(0) "" ["cat_name"]=&gt; string(10) "Case Study" ["category_nicename"]=&gt; string(10) "case-study" ["category_parent"]=&gt; int(0) }
  • 所以我们可以看到它找到了类别。我认为 $cat_id 未定义可能存在问题。另外我应该如何处理表单上的操作。
  • 是的,看我的编辑。我有$id = $term-&gt;term_id; - 这是一个错字。我已经更新了它,所以它是 $cat_id = $term-&gt;term_id; - 这应该可以解决它。
猜你喜欢
  • 2022-01-12
  • 2012-05-07
  • 1970-01-01
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
相关资源
最近更新 更多