• Hi, I am using this custom search form that has three different dropdowns that contains a list different taxonomies.

    <form role="search" method="get" id="searchform" action="<?php echo bloginfo( 'url' ); ?>">
    <input type="hidden" name="" value="" id="" />
    
    <?php echo my_dropdown('genre', 'genre'); ?>
    <?php echo my_dropdown('type', 'type'); ?>
    <?php echo my_dropdown('ayear', 'ayear'); ?>
    
    <input type="submit" id="searchsubmit" value="Submit" />
    </form>

    When submitting the form, it’s returning the ID’s rather than the slugs. This is my function,

    <?php
    
    function my_dropdown($name, $taxonomy = 'category') {
    $defaults = array(
        'taxonomy' => $taxonomy,
        'id'       => $name,
        'name'     => $name,
        'show_option_none' => ' - Select - ',
        'selected' => get_query_var($name)
    );
    
    wp_dropdown_categories($defaults);
     } 
    
    add_action('pre_get_posts', 'my_customsearch');
    function my_customsearch($query) {   
    
        if(! $query->is_main_query() || ! $query->is_search )
           return;
    
        $tax_query = array();
        $genre = (int) get_query_var('genre');
        $type = (int) get_query_var('type');
        $ayear = (int) get_query_var('ayear');
    
        // first dropdown
        if (! empty($genre) && $genre > 0) {
           $tax_query[] = array(
             'taxonomy' => 'genre',
             'field'    => 'slug',
             'terms'    => $genre
           );
        }
    
        // second dropdown
        if (! empty($type) && $type > 0) {
           $tax_query[] = array(
             'taxonomy' => 'type',
             'field'    => 'slug',
             'terms'    => $type
           );
        }
    
        // third dropdown
        if (! empty($ayear) && $ayear > 0) {
           $tax_query[] = array(
             'taxonomy' => 'ayear',
             'field'    => 'slug',
             'terms'    => $ayear
           );
        }
    
       if ( sizeof($tax_query) > 0 ) {
          $tax_query['relation'] = 'AND';
          $query->set( 'tax_query', $tax_query );
       }
    
    }
    
    // add my custom query vars
    add_filter('query_vars', 'mycustom_query_vars'); 
    
    function mycustom_query_vars($query_vars) {
      $query_vars[] = 'genre';
      $query_vars[] = 'type';
      $query_vars[] = 'ayear';
    
      return $query_vars;
     }
    
    ?>
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Output Taxonomy Slugs’ is closed to new replies.