• I’m using the following code to create a drop down box for a custom taxonomy search:

    Functions code:

    function get_terms_dropdown($taxonomies, $args){
    $myterms = get_terms($taxonomies, $args);
         $output ="<select name='TERM'>";
         foreach($myterms as $term){
              $root_url = get_bloginfo('url');
              $term_taxonomy=$term->taxonomy;
              $term_slug=$term->slug;
              $term_name =$term->name;
              $link = $term_slug;
              $output .="<option value='".$link."'>".$term_name." (".$term->count.") </option>";
         }
         $output .="</select>";
    return $output;
    }

    PHP code (placed in a php widget):

    <form action="<?php bloginfo('url'); ?>" method="get">
         <div>
    <?php
    $taxonomies = array('TAXONOMY NAME');
    $args = array('orderby'=>'name','hide_empty'=>true);
    $select = get_terms_dropdown($taxonomies, $args);
    
    $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
    echo $select;
    ?>
         <noscript><div><input type="submit" value="Term Value" /></div></noscript>
         </div></form>

    When a term is selected from the list it sends the user to a page with a URL output like: domain.com/?TERM=term1

    How can i output this instead as: domain.com/TERM/term1

    I also only want this to return posts from a specific custom post type – is there an extra bit of code i can add to get this functioning for post_type=’xyz’ ?

    Thanks in advance,
    Rob

Viewing 2 replies - 1 through 2 (of 2 total)
  • Any luck with this Rob?

    Thread Starter ProductAnatomy

    (@productanatomy)

    Well the searching within a custom post type is relatively easy.

    I just used a code similar to the following code for my news post type (replace ‘news’ with the name of you custom post type:

    function search_filter() {
    global $blog_id;
    	if (is_post_type_archive ( 'news' ) || is_singular( 'news' )) {?>
    	<li class="nav_search"><form method="get" class="search_form" action="<?php bloginfo('home'); ?>/">
    		<p>
    			<input class="text_input" type="text" value="" name="s" id="s"/>
    			<input type="hidden" name="post_type" value="news" />
    		</p>
    	</form></li>
    	<?php }
    }
    add_action('$tag','search_filter');

    For the first part of my original question I believe there are a couple of ways to do this. Firstly you could use custom rewrite rules to rewrite the urls once they’ve been searched for – switching them from domain.com/?TERM=term1 to domain.com/taxonomyname/term1 (I’m not too familiar with rewrite rules I’m afraid).

    The second option is a little easier and would be to create the custom taxonomy with the archive slug as you would like them – domain.com/taxonomyname/term1 (set the slug to ‘taxonomyname’ when creating the taxonomy). Now when you search using the drop down, WordPress should automatically rewrite the urls to domain.com/taxonomyname/term1.

    Hope this helps

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Taxonomy Dropdown search URLs’ is closed to new replies.