• Hi,

    I’ve been trying to figure out how to create a custom search form that will include a drop down list of values from a custom taxonomy.

    First of all, which file do I override? search.php, searchform.php ?
    I’ve tried both but it’s not working and I’m not sure if it’s because there is something wrong with my code, or if it’s because I’m overriding the wrong file….

    This is the code I have so far…

    *** The form:

    <form action="<?php bloginfo('siteurl'); ?>" id="searchform" method="get">
    
            <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
    
            <input type="hidden" name="post_type[]" value="centre"/>
    
    			<select name="association" id="assoc">
        				<option value="">All</option>
    				<?php
    
    				$taxonomies = array('associations');
    				$args = array(
        					'orderby'       => 'name',
        					'order'         => 'ASC',
        					'hide_empty'    => true );
    
    				$theterms = get_terms_by_post_type(array('associations'),array('centres'));
    					foreach ($theterms AS $term) :
    
    					echo "<option value =".$term->ID.">".$term->name."</option>\n";
    					endforeach;
    				?>
      			</select>
    			 <input type="image" id="searchsubmit" alt="Search" src="<?php bloginfo( 'template_url' ); ?>/images/searchicon.png" />
    </form>
    
    *** The Query:
    
    $query_args = array(
            's' => $_GET['s'],
    	 'post_type' => 'centre',
    	  'tax_query' => array(
    		array(
    			'taxonomy' => 'associations',
    			'field' => 'ID',
    			'terms' => $_GET['association']
    		)
    	)
    );
    $search = new WP_Query( $query_args );
    
    *** The results:
    if ( $search->have_posts() ) : while ( $search->have_posts() ) : $search->the_post();
    <h3><?php the_title(); ?></h3>
    <?php
    endwhile;
    endif;?>

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

  • The topic ‘HowTo Create Custom Search Form’ is closed to new replies.