Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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>
share|improve this answer
    
Awesome! Works well. It's not perfect but it's better than what I was getting before. Thanks :). –  Kwame Boame Nov 13 '11 at 23:44

Possibly this added to your form?

<input type="hidden" name="post_type" value="publication" />
share|improve this answer
    
Thanks for the reply. Where should I add it? When I change the current input type from "text" to "hidden", the input bar becomes invincible. –  Kwame Boame Nov 13 '11 at 16:27
    
You should add the whole line anywhere in the form. Don't change any of your code. See Bainternets answer below. –  Blackbird Nov 13 '11 at 17:33
    
thanks. I couldn't figure out what I was doing wrong. Thanks for your help. –  Kwame Boame Nov 13 '11 at 23:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.