How do I search inside specific taxonomies in WordPress

I am trying to search for posts in specific taxonomies but any time I search, I get results from all the other pages instead of WordPress searching within the selected category in the taxonomy.

I named my taxonomy: “publication_categories”. It contains all categories for a custom post type which I named “publication”. This is the code I have for my custom-search.php:

<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
<div class="alignleft">
<select name="taxonomy">
<option value="0">Select...</option>
<?php
$theterms = get_terms('publication_categories', 'orderby=name');
foreach ($theterms AS $term) :
    echo "<option value='".$term->slug."'".($_POST['publication_categories'] == $term->slug ? ' selected="selected"' : '').">".$term->name."</option>n";
endforeach;
?>
</select>
</div>
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" class="searchinput" />
<input type="submit" id="searchbutton" value="Search" class="btn" />
</form>

Related posts

Leave a Reply

2 comments

  1. I see a few problems with your code.

    1. you are missing post_type field in your form.
    2. the name of the taxonomy dropdown should be the name of your custom taxonomy.
    3. the form method is set to GET yet you check the selected with POST.

    so your form should look something like this:

    <form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
        <div class="alignleft">
            <select name="publication_categories">
                <option value="0">Select...</option>
                <?php
                $theterms = get_terms('publication_categories', 'orderby=name');
                foreach ($theterms AS $term) :
                    echo "<option value='".$term->slug."'".($_GET['publication_categories'] == $term->slug ? ' selected="selected"' : '').">".$term->name."</option>n";
                endforeach;
                ?>
            </select>
        </div>
        <input type="text" value="<?php the_search_query(); ?>" name="s" id="s" class="searchinput" />
        <input type="hidden" name="post_type" value="publication" />
        <input type="submit" id="searchbutton" value="Search" class="btn" />
    </form>