Advanced search form with filters for custom taxonomies

I’m trying to build a complex search form with filters for several custom taxonomies, a search terms input and a select for the number of items displayed / page.

The question has been posted before over at Advanced search form with filters for custom taxonomies and custom fields and the answer provided by Brady was an excellent starting point.

Read More

I’m going to try my best to explain the functionality, here’s the page template source https://gist.github.com/a82329d519c93d35f1f3

Basically, I have the ‘interview’ custom type and 2 taxonomies ‘interview-type’ and ‘interview-cat’ which will have filters inside the form.

There are actually 2 different forms, both containing search filters: #search-filters, which is the main one, and #type-filters, which only has 3 buttons for the ‘custom-type’ taxonomy values.

Both of these forms work the way I want them to, but there are a couple of problems still:

The biggest one is pagination: The function mytheme_page_navi is the same as the WP-PageNavi plugin, and the problem here is that as soon as I go to the second page, I lose all the filter values, and it displays posts from the ‘written’ ‘interview-type’, 10 posts / page ( which are the default values, for when the user first arrives on the page and hasn’t selected any options yet. ) Why does this happen?

Also, another problem is that because I have 2 forms, if I enter some values in the main one, and then select the ‘interview-type’ from the #type-filters, all the previous filter values are removed.

In the end, I would be grateful if someone could take a quick look over the code and point out what I’m doing wrong. I’ve been trying to wrap my head around this for days with no luck.

Related posts

Leave a Reply

1 comment

  1. The reason you are losing your filters is because you are passing them through POST in the forms. This is fine, but you will have to store these values somehow for the pagination. I believe if you used GET, you may retain the values on “page 2”, but any POST values are lost as soon as you go to the next page.

    You could store the search items in a session cookie, force the POST values for the next paginated page, or (what I recommend) use GET instead, it’s not like the search filter values are that important.

    $next_link = get_permalink();
    

    First echo out the get_permalink() value, it may retain your current args already. If not use add_query_arg() to retain them.

    $next_link = add_query_arg( 'paged', ((int)$_GET['paged'])+1), $next_link);
    // Do this as many times as you need to
    $next_link = add_query_arg( 'your_filter', $_GET['your_filter'], $next_link);
    $next_link = add_query_arg( 'your_other_filter', $_GET['your_other_filter'], $next_link);