admin管理员组文章数量:1390230
I'm rebuilding this website - on the WP platform. I'm struggling with the search area in the grey box.
I'm removing region and date so we don't have to worry about those but how can I setup the search by keyword AND sector?
I've created a custom taxonomy called "news" which will represent the sectors. As far as I know the normal search just searches the entire site for a keyword. How can I setup the search to search for a keyword but if a sector is selected only search for the keyword within that sector? And then display the results.
UPDATE:
<?php $args = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'show_last_update' => 0,
'style' => '',
'show_count' => 0,
'hide_empty' => 1,
'use_desc_for_title' => 1,
'child_of' => 0,
'feed' => '',
'feed_type' => '',
'feed_image' => '',
'exclude' => '',
'exclude_tree' => '',
'include' => '',
'hierarchical' => true,
'title_li' => '',
'show_option_none' => __('No categories'),
'number' => NULL,
'echo' => 1,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'news',
'walker' => 'Walker_Category' );
?>
<?php echo wp_list_categories($args); ?>
<?php
function Search_with_in_a_tax( &$query ) {
if ( is_search() && isset($_GET['sector_array'])) {
$tax_query = array(
array(
'taxonomy' => 'news',
'terms' => $_GET['sector_array'],
'field' => 'term_id',
)
);
//turn it into a WP_Tax_Query object
$tax_query = new WP_Tax_Query($tax_query);
$query->set("tax_query", $tax_query);
}
}
add_action('pre_get_posts', 'Search_with_in_a_tax', 1);
?>
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" size="18" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" class="btn" />
</div>
</form>
This is what I have so far. It seems to be searching the keywords and picking out the news. I can't make the sectors selectable (like the link at the top) though? How could I link the two? The search would then search within that sector (taxonomy) for the keyword.
I'm rebuilding this website - http://www.mediwales/news on the WP platform. I'm struggling with the search area in the grey box.
I'm removing region and date so we don't have to worry about those but how can I setup the search by keyword AND sector?
I've created a custom taxonomy called "news" which will represent the sectors. As far as I know the normal search just searches the entire site for a keyword. How can I setup the search to search for a keyword but if a sector is selected only search for the keyword within that sector? And then display the results.
UPDATE:
<?php $args = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'show_last_update' => 0,
'style' => '',
'show_count' => 0,
'hide_empty' => 1,
'use_desc_for_title' => 1,
'child_of' => 0,
'feed' => '',
'feed_type' => '',
'feed_image' => '',
'exclude' => '',
'exclude_tree' => '',
'include' => '',
'hierarchical' => true,
'title_li' => '',
'show_option_none' => __('No categories'),
'number' => NULL,
'echo' => 1,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'news',
'walker' => 'Walker_Category' );
?>
<?php echo wp_list_categories($args); ?>
<?php
function Search_with_in_a_tax( &$query ) {
if ( is_search() && isset($_GET['sector_array'])) {
$tax_query = array(
array(
'taxonomy' => 'news',
'terms' => $_GET['sector_array'],
'field' => 'term_id',
)
);
//turn it into a WP_Tax_Query object
$tax_query = new WP_Tax_Query($tax_query);
$query->set("tax_query", $tax_query);
}
}
add_action('pre_get_posts', 'Search_with_in_a_tax', 1);
?>
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" size="18" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" class="btn" />
</div>
</form>
This is what I have so far. It seems to be searching the keywords and picking out the news. I can't make the sectors selectable (like the link at the top) though? How could I link the two? The search would then search within that sector (taxonomy) for the keyword.
Share Improve this question edited Feb 14, 2020 at 9:30 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Feb 16, 2012 at 10:11 RobRob 1,41613 gold badges41 silver badges68 bronze badges1 Answer
Reset to default 1You can use pre_get_posts
hoot to filter the search query to selected sectors only, something like this:
function Search_with_in_a_tax( &$query ) {
if ( is_search() && isset($_GET['sector_array'])) {
$tax_query = array(
array(
'taxonomy' => 'news',
'terms' => $_GET['sector_array'],
'field' => 'term_id',
)
);
//turn it into a WP_Tax_Query object
$tax_query = new WP_Tax_Query($tax_query);
$query->set("tax_query", $tax_query);
}
}
add_action('pre_get_posts', 'Search_with_in_a_tax', 1);
Update: Put the code from above at the functions.php file of your theme and then you need to output the categories (sectors) as form fields inside your search form so try this:
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div>
<label for="s">Keyword</label>
<input type="text" size="18" value="" name="s" id="s" />
</div>
<div>
<label for="sector_array">Sectors</label>
<?php
$categories=get_categories(array('orderby' => 'name','order' => 'ASC'));
foreach ($categories as $category) {
echo '<input type="checkbox" name="sector_array[]" value="'.$category->cat_ID.'">'.$category->cat_name;
}
?>
</div>
<div>
<input type="submit" id="searchsubmit" value="Search" class="btn" />
</div>
</form>
本文标签: phpCustomising the search function
版权声明:本文标题:php - Customising the search function? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744748566a2623036.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论