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've set up a custom post type called 'properties'. These posts can be tagged with the custom taxonomies of 'area' and 'university'.

I would like to be able to have a form with a drop-down for each tag. When the form is submitted I would like to go to the URL that displays the archive of those tags.

Two questions really:

  1. What is the URL that will return the correct posts?
  2. How do you construct that URL with a form?

For some reason I thought that this would be really easy but I just can't find a way to do it. Any help is much appreciated.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

To understand how to do it you first need to know how the url query works or more specifically what query vars you need, so:

  • 'post_type' - to filter by post types.
  • 'area' - to filter by area taxonomy.
  • 'university' - to filter by university taxonomy.

so your url would look like:

//   http://www.domain.com/?post_type=properties&area=area_term&university=university_term

Now that we know we create the form with the needed fields:

function display_area_university_filter_form(){
?>
<form method="post" action="">
    <p><label for="area_t">Area: </label> <select name="area_t"><option value="">Select Area:</option>
    <?php 
        $area_terms =  get_categories('taxonomy=area'); 
        if (isset($_POST['area_t'])){
            foreach ($area_terms as $term) {
                $option = '<option value="'.$term->slug.'"';
                if ($_POST['area_t'] == $term->slug){
                    $option.= ' selected="selected"';
                }
                $option .= '>'.$term->name;
                $option .= '</option>';
                echo $option;
            }
        }else{
            foreach ($area_terms as $term) {
                $option = '<option value="'.$term->slug.'"';
                $option .= '>'.$term->name;
                $option .= '</option>';
                echo $option;
            }
        }
    ?>
    </select>
    </p>
    <p><label for="area_t">University: </label> <select name="area_t"><option value="">Select University:</option>
    <?php 
        $uni_terms =  get_categories('taxonomy=university'); 
        if (isset($_POST['univer'])){
            foreach ($uni_terms as $term) {
                $option = '<option value="'.$term->slug.'"';
                if ($_POST['univer'] == $term->slug){
                    $option.= ' selected="selected"';
                }
                $option .= '>'.$term->name;
                $option .= '</option>';
                echo $option;
            }
        }else{
            foreach ($uni_terms as $term) {
                $option = '<option value="'.$term->slug.'"';
                $option .= '>'.$term->name;
                $option .= '</option>';
                echo $option;
            }
        }
    ?>
    </select>
    </p>
    <input type="hidden" name="custom_filter" value="area_uni">
    <p><input type="submit" name="submit" value="submit"></p>
</form>
<?php }

then all that is left is just processing it:

if (isset($_POST['submit']) && isset($_POST['custom_filter']) && $_POST['custom_filter'] == "area_uni"){
    $url =  get_bloginfo('url');
    $area = $uni = '';
    if (isset($_POST['area_t'])){
        $area = '&area='.$_POST['area_t'];
    }
    if (isset($_POST['univer'])){
        $uni = '&university='.$_POST['univer'];
    }

    $url .= '/?post_type=properties'.$area.$uni;

    wp_redirect( $url );
    exit();
}
share|improve this answer
    
Hi Bainternet. Thanks so much for you help. I'm not exactly sure how to implement your solution. Does the second bit of code go directly under the first? I'm getting the error - Parse error: syntax error, unexpected '{' in /Library/WebServer/Documents/langley/wp-content/themes/langley/functions.php on line 121. That line is - if (isset($_POST['area_t']){. Thanks again. –  kdev Jul 2 '11 at 17:23
    
My bad i fixed the code, and just paste both snippets in your functions.php file. –  Bainternet Jul 2 '11 at 19:21
    
Thanks again Bainternet. I had to make a few changes to get it working properly. I've added value="" to the Select options so that they don't show up in the URL. Also, there is a } outside of the code window that confused me for a while! –  kdev Jul 3 '11 at 8:12
add comment

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.