0

我尝试了几种方法,但似乎无法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_typeof添加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 个唯一条目......所以它们在那里......

任何见解?

4

2 回答 2

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');
于 2011-11-19T01:00:41.173 回答
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; ?>

显示来自自定义查询的帖子

于 2011-11-18T04:55:21.127 回答