1

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.

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.

1 Answer 1

0

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);
4
  • It works but the problem is that since I have to use a numbered page navigation, this isn't the best solution. Thanks anyway!
    – Andrew
    Aug 29, 2012 at 13:46
  • where you create your numbered navigation (inside mytheme_page_navi) is where you would want to add any filters being used. Like I said, check to see if get_permalink() brings $_GET values with it. If it does, you are golden. Aug 29, 2012 at 14:00
  • Actually I've replaced that function altogether with paginate_links() and now it works just fine, my only complaint would be that the url looks sort of weird, but I guess there isn't much I can do about that cl.ly/image/2q3f0w0X2r3i ? Anyway, thanks a lot for helping me out with this.
    – Andrew
    Aug 29, 2012 at 14:18
  • Yeah that'll happen. You can use rewrite rules but I have never had any luck with it. Aug 29, 2012 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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