【问题标题】:Wordpress custom search with more than one post type具有多种帖子类型的 Wordpress 自定义搜索
【发布时间】:2016-11-22 04:51:59
【问题描述】:

在 WordPress 中,当您想将搜索限制为特定的帖子类型时,您可以将以下行添加到您的搜索表单 <input type="hidden" value="product" name="post_type" /> 其中 value 包含帖子类型的名称。如果您想使用两种帖子类型进行搜索怎么办?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我设法通过在 functions.php 文件中添加以下函数来做到这一点

    function searchfilter($query) {
    
    if ($query->is_search && !is_admin() ) {
        $query->set('post_type',array('post_type_1','post_type_2'));
    }
    

    返回$查询; }

    add_filter('pre_get_posts','searchfilter');

    注意$query->set('post_type',array('post_type_1','post_type_2'));这一行

    这是我将帖子类型作为参数传递的地方

    【讨论】:

    • 好朋友!
    猜你喜欢
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 2014-01-03
    • 2021-12-23
    相关资源
    最近更新 更多