1

I'm rebuilding this website - http://www.mediwales.com/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.

1 Answer 1

1

You 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>
8
  • Ok cool, thanks. So that will handle the keyword as well? How can I link that to the search form?
    – Rob
    Feb 16, 2012 at 10:34
  • 1
    As log as you set the from method to get and set the keyword field name to s it will be linked for you.
    – Bainternet
    Feb 16, 2012 at 10:42
  • Ok thanks. Works in theory I'll give it a go shortly.
    – Rob
    Feb 16, 2012 at 11:07
  • I've got the search working to a degree. I can't quite link up the sectors though. I've updated my question with what I have so far.
    – Rob
    Feb 16, 2012 at 16:21
  • 1
    i updated my answer
    – Bainternet
    Feb 16, 2012 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.