通过post_type进行Wordpress自定义搜索

时间:2011-11-18 04:25:31

标签: wordpress custom-wordpress-pages

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

我已安装“作业管理器”并创建了4个具有自定义post_type = 'jobman_job'

的作业

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

<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隐藏字段):

<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添加到查询数组中:

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);

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

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

任何洞察力?

2 个答案:

答案 0 :(得分:0)

正如codex所解释的,在获取新数据后,您需要用新数据替换循环,如本示例所示

<?php if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
 <?php setup_postdata($post); ?>

 <div class="post" id="post-<?php the_ID(); ?>">
 <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
   <?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
 </div>
 <?php endforeach; ?>
 <?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
 <?php endif; ?>

Displaying posts from custom query

答案 1 :(得分:0)

我只是按原样保留html:

<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>

并将以下内容添加到我的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');