admin管理员组文章数量:1430551
I am doing a WordPress site and use a custom post type 'studentinfo'. I want to build a search form and search template that will retrieve only search result from the specific post type studentinfo. Is it possible?
I am doing a WordPress site and use a custom post type 'studentinfo'. I want to build a search form and search template that will retrieve only search result from the specific post type studentinfo. Is it possible?
Share Improve this question edited Apr 24, 2019 at 11:08 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Mar 7, 2016 at 8:40 Moshiur RahmanMoshiur Rahman 14 bronze badges1 Answer
Reset to default 0Yes this is possible.
There are numerous ways in which you can achieve this, either by creating a custom form and workflow or by overriding the default search template in WordPress and supply a request parameter for which can be used to identify the search request and filter the post types that should be searched.
Here's how to override the WordPress search form template that gets output from using the get_search_form()
function.
Create a php file called searchform.php
in your theme and place the following code:
<form role="search" method="get" id="searchform"
class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div>
<label class="screen-reader-text" for="s"><?php _x( 'Search for:', 'label' ); ?></label>
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit"
value="<?php echo esc_attr_x( 'Search', 'submit button' ); ?>" />
</div>
</form>
And in your functions.php
file insert the following code:
function filter_search_post_type($query) {
if ( $query->is_search && ! is_admin() && ! empty($_GET['search_post_type']) ) {
$query->set( 'post_type', array($_GET['search_post_type']) );
}
}
return $query;
}
add_filter('pre_get_posts','filter_search_post_type');
Where you want the search form to appear in your theme do the following:
<?php get_search_form(); ?>
Each time a search is made using the search form, we send along a hidden input value of studentinfo
with a request variable name of search_post_type
.
Within the pre_get_posts
hook we check to see if this is a search request and if the search_post_type
request variable is set and not empty. If so we proceed to set the post_type
for the query to that of the value studentinfo
.
The only downside to this approach is that the form will be restricted to searching for posts within the studentinfo
post_type due to the hidden input value we're passing back with the request.
Alternatively instead of creating a file called searchform.php
you can create a file called studentinfo-searchform.php
and paste the same form code above into that file, saving it into your theme directory.
Then where you want the form to show in your template, you will call:
<?php get_template_part('studentinfo-searchform'); ?>
This will enable you to only show the search form for studentinfo
when you need to and as such not effect default search ability.
Of course there are even more ways you can go about this but let's get you started with some basics methods first.
本文标签: customizationWordPress search for specific Post Type
版权声明:本文标题:customization - WordPress search for specific Post Type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745558257a2663315.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论