1

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.

I have installed "Job Manager" and created 4 jobs which have a custom 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>

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>

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

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

Any Insight?

1 Answer 1

0

NOT TESTED!

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

 add_filter('pre_get_posts','mySearchFilter');
10
  • The only difference appears to be that you've made the value a select value instead of a hidden value - still gives the same result - shows all posts regardless of type... Commented Nov 18, 2011 at 5:22
  • just edited the answer... worked perfect for me Commented Nov 18, 2011 at 5:32
  • Ok... That works but it works across all searches! I am trying to have a custom search on the /jobs page that only searches for jobman_job post_type's - every other search should search all posts... Commented Nov 18, 2011 at 5:38
  • use the form I typed before, and then make a conditional that display this code if post_type, or maybe create an extra search class and call it in the action of your form... I don't see ur escenario so I cannot be that helpful :) Commented Nov 18, 2011 at 5:43
  • Working now - I copied search.php to job-search.php...Changed the form action to /job-search.php...Added your code above and it is now working as expected Commented Nov 18, 2011 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.