admin管理员组文章数量:1122832
My website has a custom post type. I'd like to allow users the ability to search within the custom post type. So I'd like the search results to only include posts of that particular post type.
I'd like to also offer normal search. That is, I'd like the site to have forms that have the standard wordpress search, allowing users the ability to search within the native post types (Post and Page).
Because I want to offer a normal search alongside a customised search, I can't filter the search query with the 'pre_get_posts' hook. So the best option is to custom the search form.
for the post type mypost
with the slug myposts
, I have this form:
<form role="search" method="get"
id="searchform block"
class="search-form"
action="<?php echo esc_url(home_url('/')); ?>"
>
<div class="relative">
<label class="screen-reader-text" for="s"><?php esc_html_e('Search for:', 'text-domain'); ?></label>
<input id="search-field"
type="search"
class="search-field outline-none"
value="<?php echo esc_html(get_search_query()); ?>" name="s" />
<input type="hidden" name="post_type" value="myposts" />
<button class="search-button cursor-pointer absolute right-0 top-0 bottom-0 m-auto py-5 px-4 ml-4" type="submit" id="searchsubmit">
<span class="sr-only"><?php esc_html_e('Search', 'text-domain'); ?></span>
</button>
</div> </form>
In the input parameter value
, I've tried both mypost
and myposts
.
For either value
, a search will return zero results.
Instances of the custom post type do exist.
So: how can I create a working form that will search only for results in the target custom post type?
My website has a custom post type. I'd like to allow users the ability to search within the custom post type. So I'd like the search results to only include posts of that particular post type.
I'd like to also offer normal search. That is, I'd like the site to have forms that have the standard wordpress search, allowing users the ability to search within the native post types (Post and Page).
Because I want to offer a normal search alongside a customised search, I can't filter the search query with the 'pre_get_posts' hook. So the best option is to custom the search form.
for the post type mypost
with the slug myposts
, I have this form:
<form role="search" method="get"
id="searchform block"
class="search-form"
action="<?php echo esc_url(home_url('/')); ?>"
>
<div class="relative">
<label class="screen-reader-text" for="s"><?php esc_html_e('Search for:', 'text-domain'); ?></label>
<input id="search-field"
type="search"
class="search-field outline-none"
value="<?php echo esc_html(get_search_query()); ?>" name="s" />
<input type="hidden" name="post_type" value="myposts" />
<button class="search-button cursor-pointer absolute right-0 top-0 bottom-0 m-auto py-5 px-4 ml-4" type="submit" id="searchsubmit">
<span class="sr-only"><?php esc_html_e('Search', 'text-domain'); ?></span>
</button>
</div> </form>
In the input parameter value
, I've tried both mypost
and myposts
.
For either value
, a search will return zero results.
Instances of the custom post type do exist.
So: how can I create a working form that will search only for results in the target custom post type?
Share Improve this question edited Sep 16, 2022 at 14:50 atiquratik 1218 bronze badges asked Sep 13, 2022 at 6:06 bobbob 2491 gold badge6 silver badges16 bronze badges 2 |1 Answer
Reset to default 0First, ensure that your custom post type is public and searchable
Second, you can't use post_type
url query var, it will be strip, its reserve for custom post type archive.
you can however simply do a custom post type search with URL like this.
domain.com/custom-post-type-rewrite-slug/?s=My+Search+Term
so in your form, the action should be
<?php echo esc_url(home_url('/custom-post-type-rewrite-slug/')); ?>
add remove this hidden input for post_type
<input type="hidden" name="post_type" value="myposts" />
Additionally, you can also set your own query var then modify the query via pre_get_posts
action hook so you can perform search on tld with one or more post_type
support.
Simply add query var first
add_filter( 'query_vars', function( $vars ) {
$vars[] = 'ptype';
return $vars;
});
Now, modify the query if ptype
is present in the URL
add_action('pre_get_posts', function( $query) {
if ( !is_admin() && isset( $query->query['s'] ) && isset( $query->query['ptype'] ) ) {
$query->set('post_type', $query->query['ptype']);
}
return $query;
});
Once added, you can do a search like bellow;
for searching on single post type
domain.com/?ptype=whatever&s=search+term
for searching on multiple post type
domain.com/?ptype[]=whatever&ptype[]=post&s=search+term
so if you want to support multiple post type on your search form, you can simply add multiple hidden input
<input type="hidden" name="ptype[]" value="whatever" />
<input type="hidden" name="ptype[]" value="post" />
本文标签: How do I create a search form that searches only within a custom post type
版权声明:本文标题:How do I create a search form that searches only within a custom post type? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281072a1926202.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
exclude_from_search
is set tofalse
for your custom post type? This might be helpful: wordpress.stackexchange.com/questions/29447/… – atiquratik Commented Sep 13, 2022 at 8:11