Reputation: 733
I made a custom post type plugin to display team member in wordpress name team to display custom post type I made archive-team.php
and single-team.php
all that works great and independent of theme used.
Now my problem is that I want to include custom post type in search also but I think there is no such thing searh-team.php
and to display search template for custom post type i have to modify search.php
which varies from theme to theme.
So can anyone point me in right direction to make search template independent of theme.
Regards
Upvotes: 1
Views: 3723
Reputation: 142
Follow these 3 steps to achieve custom post search.
Lets say your custom post type name is employee_member.
Add Below code into function.php
function template_chooser($template) { global $wp_query; $post_type = get_query_var('post_type'); if( $wp_query->is_search && $post_type == 'employee_member' ) { return locate_template('employee-search.php'); // redirect to archive-search.php } return $template; } add_filter('template_include', 'template_chooser');
Create file employee-search.php
/* Template Name: Custom Search */
<?php get_header(); ?>
<div class="contentarea">
<div id="content" class="content_right">
<h3>Search Result for : <?php echo "$s"; ?> </h3>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="posts">
<article>
<h4><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title(); ?></a></h4>
<p><?php the_exerpt(); ?></p>
<p align="right"><a href="<?php the_permalink(); ?>">Read More</a></p>
<span class="post-meta"> Post By <?php the_author(); ?>
| Date : <?php echo date('j F Y'); ?>
</span>
</article><!-- #post -->
</div>
</div><!-- content -->
</div><!-- contentarea -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<div>
<h3>Search Employee Member</h3>
<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="text" name="s" placeholder="Search Products"/>
<input type="hidden" name="post_type" value="employee_member" />
<input type="submit" alt="Search" value="Search" />
</form>
</div>
Upvotes: 1
Reputation: 1102
You can add this in your functions.php
:
function filter_search($query) {
if ($query->is_search) {
$query->set('post_type', array('post', 'team')); // Add all post types you want results for.
};
return $query;
};
add_filter('pre_get_posts', 'filter_search');
Upvotes: 0
Reputation: 431
To do this, a good practice is to use pre_get_posts() to alter the search query. This code can be dropped in the functions.php of the theme or directly into a plugin.
<?php
// Call our function my_search()
add_filter( 'pre_get_posts', 'my_search' );
function my_search($query) {
// Check if we are on the front end main search query
if ( !is_admin() && $query->is_main_query() && $query->is_search() ) {
// Change the post_type parameter on the query
$query->set('post_type', 'team');
}
// Return the modified query
return $query;
}
?>
The search will now be queried only your custom post type "team". If you would like the research also integrates post and pages, you can modify the following line:
<?php
$query->('post_type', array('team', 'post', 'page'));
?>
If you use the add_filter() in a PHP Class, be careful to declare your filter like this:
<?php
add_filter( 'pre_get_posts', array( $this, 'my_search' ) );
?>
Upvotes: 0