2011-11-18 145 views
0

我試過了幾個方法,但我似乎無法過濾自定義post_types從我的搜索結果,並希望有人可以幫助。WordPress的自定義搜索post_type

我已經安裝了「任務管理器」,並創造了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)和I加入jobman_jobpost_type到查詢數組:

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

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

我在做什麼錯?我已檢查wp_posts表中的post_type列,並且我有4個唯一條目...所以它們在那裏...

Any Insight?

回答

0

食品法典解釋說,獲得新數據後,你需要用新的數據來代替循環,就像這個例子

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

+0

沒有工作。 反正我發現了另一種解決方法 –

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