Correct way to use a form to to filter custom posts by taxonomy terms?

This is a continuation of this post.

I’ve built my form and managed to get the correct taxonomy terms listed as a dropdown, but I’m wondering what action I should be using in my form and how I can recall the options selected in the same form on the results page?

This is the code I’ve written so far which sets query_posts() so that it fetches the custom posts ‘ftevent’ that have the taxonomy terms ‘type’, ‘period’ and ‘duration’ set to something the user chooses:

Pastie link

And this is the function osu_list_terms() that you’ll see in that code which tries to recall the options selected in each dropdown:

function osu_list_terms($osu_tax, $osu_recall) {
    // list terms in fttype taxonomy
    $taxonomy = $osu_tax;
    $tax_terms = get_terms($taxonomy);
    foreach ($tax_terms as $tax_term) {
        echo '<option' . $osu_recall . '>' . $tax_term->name . '</option>';
    }
}

Now, this is a work in progress, so I’m sure there are a few other problems I’ll come up against (such as ordering the custom posts by the custom field ‘StartEventDate’), but at this point, I’m trying to work out how WordPress works with something like this form.

I’m used to using

<?php echo $_SERVER['PHP_SELF']; ?>

for forms so that the form submits to the same page. I was hoping I could do the same with this, but WordPress redirects to a URL like this and uses the archive.php template instead of the current page template I’ve inserted this form into:

/?fttype=Group+walks&ftperiod=Weekday&ftduration=Half-day&showfilter=true

Is there a ‘correct’ way to do this? I’m a little confused as to how I can style a template for the search results as well as the results are displayed using the archive page (which is also used by tag-cloud results).

Any tips you can offer?

Thanks,

osu

1 Answer
1

If you leave the action attribute empty, the form will post back to the current URL.

However, if your method attribute is get, the form will post back to current URL, minus the current query string.

If you want to keep the current query string, you can use hidden inputs to replicate the current parameters.

And never trust PHP_SELF!

Leave a Comment