-1

Searching is not working with CPT with custom taxonomy

<form id="searchform" method="get" action="<?php bloginfo('url'); ?>">
        <input type="text" name="s" id="s" size="15" />
        <?php //wp_dropdown_categories('show_option_none=Select category&taxonomy=type'); ?>
        <?php wp_dropdown_categories('depth=0&orderby=name&hide_empty=1&show_option_all=Search Everything&name=course&taxonomy=type'); ?>
        <input type="submit" value="Search" />
    </form>

But with default post and category is working with this code

<form id="searchform" method="get" action="<?php bloginfo('url'); ?>">
    <input type="text" name="s" id="s" size="15" />
    <?php //wp_dropdown_categories('show_option_none=Select category&taxonomy=type'); ?>
    <?php wp_dropdown_categories('depth=0&orderby=name&hide_empty=1&show_option_all=Search Everything'); ?>
    <input type="submit" value="Search" />
1
  • @Jack Johansson, on search page not found error using <?php wp_dropdown_categories('name=course&taxonomy=type&depth=0&orderby=name&hide_empty=1&show_option_all=Search Everything'); ?> Commented Oct 5, 2018 at 9:26

1 Answer 1

0

Working drop down in search for CPT

<?php

  $categories = get_categories('taxonomy=custom-tax');

  $select = "<select name='cat' id='cat' class='postform'>n";
  $select.= "<option value='-1'>Select category</option>n";

  foreach($categories as $category){
    if($category->count > 0){
        $select.= "<option value='".$category->slug."'>".$category->name."</option>";
    }
  }

  $select.= "</select>";

  echo $select;
?>

<script type="text/javascript"><!--
    var dropdown = document.getElementById("cat");
    function onCatChange() {
        if ( dropdown.options[dropdown.selectedIndex].value != -1 ) {
            location.href = "<?php echo home_url();?>/category/"+dropdown.options[dropdown.selectedIndex].value+"/";
        }
    }
    dropdown.onchange = onCatChange;
--></script> 

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.