admin管理员组文章数量:1415111
I want to create Three Filters in the search form with three different filters as (Tags, categories and posts). Is it possible to create those filters? If possible means how can I create that?
Basically, am not good at PHP. So, Please help me out of this.
Thanks.
I want to create Three Filters in the search form with three different filters as (Tags, categories and posts). Is it possible to create those filters? If possible means how can I create that?
Basically, am not good at PHP. So, Please help me out of this.
Thanks.
Share Improve this question asked Aug 20, 2019 at 5:01 saicharansaicharan 32 bronze badges1 Answer
Reset to default 0Here is one to create a search form with tag and category filters. For filtering posts this form uses the default text input way, where user can type some search phrase that is matched against post titles and content.
1) Added to theme's functions.php
function get_tag_ids_and_names() {
$out = array();
$arga = array(
// check https://codex.wordpress/Function_Reference/get_tags for available params
);
$terms = get_tags( $arga );
if ( $terms ) {
foreach ( $terms as $term ) {
$out[$term->term_id] = $term->name;
}
}
return $out;
}
function get_category_ids_and_names() {
$out = array();
$arga = array(
// check https://developer.wordpress/reference/functions/get_categories/ for available params
);
$terms = get_categories( $arga );
if ( $terms ) {
foreach ( $terms as $term ) {
$out[$term->term_id] = $term->name;
}
}
return $out;
}
function render_select_options( array $options ) {
if ( $options ) {
foreach ( $options as $value => $name ) {
printf(
'<option value="%s">%s</option>',
esc_attr( $value ),
esc_html( $name )
);
}
}
}
/**
* Custom tempalte function that renders a search form
* text input for searching posts by search phrase, searches by title and content by default
* optional dropdown select filter for post tag
* optional dropdown select filter for category
* searches only posts with post post_type
*/
function search_form_with_fitlers() {
$tags = get_tag_ids_and_names();
$categories = get_category_ids_and_names();
printf(
'<form id="search-with-filters" method="GET" action="%s">
<input type="text" name="s" value="%s">
<select name="post_tag"><option value="">--</option>%s</select>
<select name="category"><option value="">--</option>%s</select>
<input type="hidden" name="post_type" value="post">
<input type="submit" value="%s">
</form>',
home_url( '/' ),
get_search_query(),
render_select_options( $tags ),
render_select_options( $categories ),
esc_html__( 'Search', 'text-domain' )
);
}
You could also do the things done in search_form_with_fitlers
function in your searchform.php
file.
2) use in post / page templates or where needed
<?php search_form_with_fitlers(); ?>
P.s. You can find many good beginner level tutorials and courses online for learning PHP, if you haven't searched or started one yet. The PHP manual has also an introduction to the language, https://www.php/manual/en/getting-started.php, which you can have a look at.
本文标签: Search with filters
版权声明:本文标题:Search with filters 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745225177a2648573.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论