通过post_type的WordPress自定义搜索 [英] Wordpress Custom Search by post_type

查看:289
本文介绍了通过post_type的WordPress自定义搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了几种方法,但似乎无法从搜索结果中过滤自定义 post_types ,希望有人能提供帮助。

I've tried a couple of methods but I cannot seem to filter custom post_types from my search results and was hoping someone could help.

我已经安装了作业管理器并创建了4个作业,这些作业具有自定义的 post_type ='jobman_job'

I have installed "Job Manager" and created 4 jobs which have a custom post_type = 'jobman_job'

我试图创建一个手动搜索表单,并将隐藏值设置为 post_type = jobman_job ,但它仍返回所有帖子。

I tried to create a manual search form and set a hidden value of post_type = jobman_job but it still returned all posts.

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" value=""/>
<input type="hidden" name="post_type" value="jobman_job" />
<input type="submit" id="searchsubmit" value="Search" />
</form>

然后我尝试创建自定义搜索页面,并将搜索重定向到此页面,如下所示(即添加了 page_id 隐藏字段):

I then tried creating a custom search page and redirecting the search to this page as follows (i.e added page_id hidden field):

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" value=""/>
<input type="hidden" name="page_id" value="123" />
<input type="hidden" name="post_type" value="jobman_job" />
<input type="submit" id="searchsubmit" value="Search" />
</form>

然后在自定义搜索页面中,我添加了以下代码(根据wordpress指南- http://codex.wordpress.org/Creating_a_Search_Page ),我添加了 post_type jobman_job 的code>到查询数组:

And then in the custom search page, I added the following code (as per wordpress guide - http://codex.wordpress.org/Creating_a_Search_Page) and I added the post_type of jobman_job to the query array:

global $query_string;

$query_args = explode("&", $query_string);
$search_query = array('post_type' => 'jobman_job');

foreach($query_args as $key => $string) {
    $query_split = explode("=", $string);
    $search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach

$search = new WP_Query($search_query);

它仍然显示所有帖子...

And it still displays all posts...

我在做什么错?我已经检查了 wp_posts 表中的 post_type 列,我有4个唯一的条目...所以它们在那里。 ..

What am I doing wrong? I have checked the post_type column in the wp_posts table and I have 4 unique entries...so they are there...

有什么见解?

推荐答案

我只是离开了html

I simply left the html as is:

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
  <input type="text" name="s" id="s" value=""/>
  <input type="hidden" name="post_type" value="jobman_job" />
  <input type="submit" id="searchsubmit" value="Search" />
</form>

并将以下内容添加到我的function.php

and added the following to my functions.php

function mySearchFilter($query) {

    if (isset($_GET['post_type']) && $_GET['post_type'] == 'jobman_job') {
        $post_type = 'jobman_job';
    } else {
        $post_type = 'any';
    }
    if ($query->is_search) {
            $query->set('post_type', $post_type);
    };
    return $query;
};

add_filter('pre_get_posts','mySearchFilter');

这篇关于通过post_type的WordPress自定义搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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