$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>templates - Searching multiple custom post types and pages|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

templates - Searching multiple custom post types and pages

matteradmin8PV0评论

I'm using bainternet's method for searching custom post_types and it works great. However, I've recently been requested to return more than one "specific" post type and perhaps individual pages for a support section on our site.

I thought adding additional hidden fields would do it, but that didn't work. And I have no clue how this will work with pages.

Any help would be appreciated!


In the template:

<form action="/" id="searchform" method="get" role="search">
    <div><label for="s" class="screen-reader-text">Search for:</label>
    <input type="text" id="s" name="s" value="">
    <input type="submit" value="Search" id="searchsubmit">

    <input type="hidden" name="post_type" value="software" />
    </div>
</form>

In functions.php:

/* Custom Search Queries */
function SearchFilter($query) {
$post_type = $_GET['post_type'];
if (!$post_type) {
    $post_type = 'any';
}
if ($query->is_search) {
    $query->set('post_type', $post_type);
};
return $query;
} 

I'm using bainternet's method for searching custom post_types and it works great. However, I've recently been requested to return more than one "specific" post type and perhaps individual pages for a support section on our site.

I thought adding additional hidden fields would do it, but that didn't work. And I have no clue how this will work with pages.

Any help would be appreciated!


In the template:

<form action="http://apollo.website/" id="searchform" method="get" role="search">
    <div><label for="s" class="screen-reader-text">Search for:</label>
    <input type="text" id="s" name="s" value="">
    <input type="submit" value="Search" id="searchsubmit">

    <input type="hidden" name="post_type" value="software" />
    </div>
</form>

In functions.php:

/* Custom Search Queries */
function SearchFilter($query) {
$post_type = $_GET['post_type'];
if (!$post_type) {
    $post_type = 'any';
}
if ($query->is_search) {
    $query->set('post_type', $post_type);
};
return $query;
} 
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Mar 22, 2011 at 16:26 Zach ShallbetterZach Shallbetter 1,2147 gold badges22 silver badges45 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 19

change

<input type="hidden" name="post_type" value="software" />

to

<input type="hidden" name="post_type[]" value="software" />
<input type="hidden" name="post_type[]" value="books" />

i have to run but this should work , just add as many hidden fields as you need for each post type

You could do it like this to switch between a singular set type and any..

<input type="hidden" name="post_type" value="software" />
<label for="post_type">Search all</label>
<input type="checkbox" name="post_type" value="any" />

Unchecked searches your specific type, checked sets the post type to any..

To me works that solution (same as Bainternet's, but without "[]" after "post_type"):

  <input type="hidden" name="post_type" value="photo">
  <input type="hidden" name="post_type" value="video">

And to get a specific template for search results based on the post type(s) (on functions.php):

function search_template_chooser($template) {
  global $wp_query;
  $post_type = get_query_var('post_type');
  if( $wp_query->is_search && $post_type == 'photo' || $wp_query->is_search && $post_type == 'video' ) {
    return locate_template('search-media.php');
  }
  return $template;
}

add_filter('template_include', 'search_template_chooser');
Post a comment

comment list (0)

  1. No comments so far