Customising the search function?

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?

Read More

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.

Related posts

Leave a Reply

1 comment

  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>