WordPress-仅在自定义帖子类型中搜索 [英] Wordpress - Search in Custom Post Type Only

查看:109
本文介绍了WordPress-仅在自定义帖子类型中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有博客和自定义post_type视频(命名为video)的网站。连同附加的各种分类法(视频类别,视频标签等)

I have a website with a blog and a custom post_type for Videos (named video). Along with various taxonomies attached to it (Video Categories, Video Tags, etc.)

我正试图建立一个搜索功能,以仅搜索视频分类法,而另一个仅搜索博客分类法。这些页面中的每个页面都会有一个搜索框,以缩小搜索结果的范围。

I am trying to set up a search function to search just the video taxononmy and another to search just the blog taxonomy. There will be a search box in each of these pages to slim down the results.

这是我到目前为止所做的。

Here is what I have done so far.

<aside id="sub-search" class="widget widget_search">
        <form class="search-form" action="http://www.studiobenna.com/jf/" method="get" role="search">
            <label>
                <span class="screen-reader-text">Search for:</span>
                <input class="search-field" type="search" name="s" value="long" placeholder="Search Videos">
            </label>
            <input type="hidden" name="post_type" value="video" />
            <input class="search-submit" type="submit" value="Search">
        </form>
    </aside>

其中以url结尾的结果如下: http://example.com/?s=video&post_type=video

Which results in the url ending in: http://example.com/?s=video&post_type=video

但这不仅仅过滤视频分类法。我有一个引用post_type = post的常规博客搜索。

But this doesn't filter only the video taxonomy. I have one that references post_type=post for the regular blog search.

在URL中查询Wordpress搜索功能以仅返回一种帖子类型的正确方法是什么?我正在使用WP扩展搜索插件,以便在屏幕上方的搜索框可以搜索整个网站。

What is the correct way to query the Wordpress search function in the URL to only return one post type? I am using the WP Extended Search plugin to allow a search box at the top right of the screem to search the entire site.

我还希望这些搜索仅限于帖子类型,但也可以选择任何类别以及附加到它们的标签(我不知道这是否是任何额外的步骤)。

I also want these searches to be limited to post type but also pickup any categories and tags attached to them (I don't know if this is any extra step).

我正在做的示例在此处 http://www.studiobenna.com/jf/?page_id=8 。如果您在此处键入Blog,则应该只有一个结果标题为 Great Western Loop,而其他结果又回来了。

An example of what I am doing is here http://www.studiobenna.com/jf/?page_id=8 in the search box next to browse. If you type in Blog here there should be only one result title "Great Western Loop" but others come back.

我已经尝试将其添加到我的function.php中:

I have tried adding this to my functions.php:

function mySearchFilter($query) {
    $post_type = $_GET['post_type'];
    if (!$post_type) {
        $post_type = 'any';
    }
    if ($query->is_search) {
        $query->set('post_type', $post_type);
    };
    return $query;
};

add_filter('pre_get_posts','mySearchFilter');

但这不起作用。
我还尝试将它添加到if。(have_posts)循环之前的search.php页面中:

But it doesn't work. I also tried adding this to the search.php page right before the if (have_posts) loop:

<?php

            if(isset($_GET['post_type'])) {
                $type = $_GET['post_type'];
                $args = array( 'post_type' => $type );
                $args = array_merge( $args, $wp_query->query );
            query_posts( $args );    
            }
        ?>

什么都没做。

推荐答案

除了查询的post_type集以外,其他所有内容都是正确的。

Everything is correct except the post_type set of the query.

这将在您的情况下正确运行:

This will work correctly for your case:

function mySearchFilter( $query ) {
    $post_type = $_GET['post_type'];
    if (!$post_type) {
       $post_type = 'any';
    }
    if ( $query->is_search ) {
       $query->set( 'post_type', array( esc_attr( $post_type ) ) );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'mySearchFilter' );

这篇关于WordPress-仅在自定义帖子类型中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆