How to go about combining dropdowns / filter queries?

I’m coming up short with my googling here. (As ‘filter’ in wordpress always turns up more content related info).

Anyway, my situation is this: I have Categories, Tags, Authors and Archive dropdowns on my blog page (using the built in wordpress dropdown functionality (http://codex.wordpress.org/Function_Reference/wp_dropdown_categories, for example).

Read More

I want to be able to combine these filters; such that if you choose a category Hats, and then when you have your list of results if you choose Blue from the dropdown you would get anything that matched both Blue and Hats. (Or if you choose a month, you’d get the posts that matched Blue and Hat from your chosen month.)

I know there has to be a way to do this. But I’m coming up short even figuring out where to start. (I’m certain there was a plugin that did this that I wanted to look at, but I can’t even find that.)

So… If anyone has any ideas about how to start this that would be amazing. Or even just an example plugin I can look at to glean some info (I really want to code this myself so I understand what is going on).

Related posts

1 comment

  1. To get you started: you can make a custom form (I never worked with wp_dropdown_categories). When the form is submitted, it gets the input variables and uses them in the query.

    The form could be something like:

    <form action="http://example.com/resultspage" method="post">
    <select name="categories">
       <option value="categoryname1"></option>
       <option value="categoryname2"></option>
       <option value="categoryname3"></option>
    </select>
    <select name="tags">
       <option value="tagname1"></option>
       <option value="tagname2"></option>
       <option value="tagname3"></option>
    </select>
    <select name="authors">
       <option value="authorname1"></option>
       <option value="authorname2"></option>
       <option value="authorname3"></option>
    </select>
    <select name="archives">
       <option value="archivesname1"></option>
       <option value="archivesname2"></option>
       <option value="archivesname3"></option>
    </select>
    </form>
    

    And then on the resultspage

    $category = $_POST['categories'];
    $tag = $_POST['tags'];
    $author = $_POST['authors'];
    $archive = $_POST['archives'];
    
    $args = array(
        'category' => $category,
        'tag' => $tag,
        'author' => $author,
        'posts_per_page' => -1 // Number of posts per page, -1 is show all 
    );
    
    $query = new WP_Query($args);
    

    A few notes. First, this is not tested. Second, check the codex on WP_Query how to use these (and other) arguments and what kind of input they need. For instance, you can use 'tag' and input the tag slug, but you can also use 'tag_id' and use the tag ID as input. Then you would change the option value of the tags select field. Third: I don’t know what you would like to do with the archives. Would you like to display posts from a certain month? This can be achieved with a meta_query (you can find documentation in the WP codex).

    Is this a good start? Let me know if it worked out.

Comments are closed.