0

My problem is this: I have 3 custom taxonomies, let's say 'author', 'title' and 'editor', each one applied to regular posts. Let's say I have post_1 that has in the 'author' field 'jorge borges' and post_2 that has 'ray bradbury'.

I'm trying to have a search form with a drop down menu containing the three taxonomies and a text field, so that if I select, i.e., 'author' and search for 'jorge borges', the result will be post_1.

The other two taxonomies should work like this as well.

I couldn't find anything similar, as many questions concern the creation of a drop down menu with all instances of a taxonomy, which is not what I want. I want a drop down menu with taxonomy categories, not values.

Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
  • Please confirm - Do you want 3x dropdowns? That is one for each Taxonomy, listing all of the available Terms for that Taxonomy. If you are unsure of what I mean, [This site](http://www.dynedrewett.com/the-team/) is a good example of that technique (although it's only 2x Taxonomy, not 3x). – David Gard Jan 30 '14 at 09:27
  • Hi David, I just want one dropdown menu containing the three taxonomies, without listing the terms, just their names. This should work as a filter for the free text area. Hope it's clearer now.. – ilariaroglieri Jan 30 '14 at 10:03
  • So if you select the Taxonomy `author`, when you type and hit `Search` it should only search for Terms in that `Taxonomy` to find matching Posts? Interesting scenario, not one that I've come across before, but it should be doable. I'll run some tests... – David Gard Jan 30 '14 at 10:14
  • Yeah, that's exactly what I'm looking for! Hope you will come up to something :-) – ilariaroglieri Jan 30 '14 at 10:20

1 Answers1

0

Ok, here is something that I have come up with and tested on my site.

Note that this is quite raw (I.e. no styling), and you'll probably need to add some resiliency to it just in case you get some unexpected results, but that is all up to you I'm afraid.

Here is the search form. I've not added an action because I don't know where you want to redirect the form. However, by default you will be directed back to the same page, so you can just query the posts there.

<form method="POST">
    <h3>Search for posts</h3>
<?php
    $taxonomies[] = get_taxonomy('author');
    $taxonomies[] = get_taxonomy('title');
    $taxonomies[] = get_taxonomy('editor');
    $options = array();

    if(!empty($taxonomies)) : foreach($taxonomies as $taxonomy) :
            if(empty($taxonomy)) : continue; endif;
            $options[] = sprintf("\t".'<option value="%1$s">%2$s</option>', $taxonomy->name, $taxonomy->labels->name);
        endforeach;
    endif;

    if(!empty($options)) :
        echo sprintf('<select name="search-taxonomy" id="search-taxonomy">'."\n".'$1%s'."\n".'</select>', join("\n", $options));
        echo '<input type="text" name="search-text" id="search-text" value=""></input>';
        echo '<input type="button" name="search" id="search" value="Search"></input>';
    endif;
?>
</form>

Now, add this just before you output your posts -

if(!empty($_POST['search-text'])) :
    $args = get_search_args();
    query_post($args);
endif;

Finally, add this to your function.php so that you can go grab the relevant $args

function get_search_args(){

    /** First grab all of the Terms from the selected taxonomy */
    $terms = get_terms($_POST['search-taxonomy'], $args);
    $needle = $_POST['search-text'];

    /** Now get the ID of any terms that match the text search */
    if(!empty($terms)) : foreach($terms as $term) :

            if(strpos($term->name, $needle) !== false || strpos($term->slug, $needle) !== false) :
                $term_ids[] = $term->term_id;
            endif;

        endforeach;
    endif;

    /** Construct the args to use for quering the posts */
    $args = array(
        'order'         => ASC,
        'orderby'       => 'name',
        'post_status'   => 'publish',
        'post_type'     => 'post',
        'tax_query'     => array(
            array(
                'taxonomy'  => $_POST['search-taxonomy'],
                'field'     => 'term_id',
                'terms'     => $term_ids,
                'operator'  => 'IN'
            )
        )
    );

    return $args();

}
David Gard
  • 11,225
  • 36
  • 115
  • 227
  • I'm trying to paste this code inside my website, but I'm not sure where do I have to paste this piece: if(!empty($_POST['search-text'])) : $args = get_search_args() query_post($args); endif; – ilariaroglieri Jan 30 '14 at 18:08
  • On the template of the page that loads when you run a search. I'm assuming you don't have `search.php`, so probably `index.php`. – David Gard Jan 31 '14 at 09:16
  • ok, I'll try that. I suppose that it has to be the same page that i put in the action field of the form, right? – ilariaroglieri Jan 31 '14 at 10:22
  • Correct, that is were it should go. – David Gard Jan 31 '14 at 10:39
  • It's not working... The search just ignores the taxonomy part and performs the search as usual. I.e. 'Authors' taxonomy have values 'gio ponti' or 'aavv'. [Test page](http://www.portaluppi.org/test-search-2/) here – ilariaroglieri Feb 02 '14 at 10:11
  • Are you definitely using `POST` for the form method, not `GET`? If so, output the value of `$_POST['search-taxonomy']` and `$_POST['search-text']` before the query to check that they are correct. Let me know... – David Gard Feb 02 '14 at 17:14
  • I tried everything.. I'm going crazy! perhaps would you mind giving a look inside the website? I can give the login data via mail – ilariaroglieri Feb 02 '14 at 18:51
  • Ok I prepared 3 pastebins with the three pages involved: [ http://pastebin.com/wNmkiEVQ ](functions.php) [ http://pastebin.com/ie8Cwi47 ](search_form.php) [ http://pastebin.com/WA0jrT7b ](search.php) Let me know of something arises... – ilariaroglieri Feb 03 '14 at 10:46
  • Hi @david-gard, I've eventually been able to make it work with your code, which is great. I would like, however, to ask you another thing: it appears it works now only for single words and not for sentences. Any idea of how to manage this? [link] http://www.portaluppi.org/category/biblioteca/ Try typing in Autore "Politecnico" and "Politecnico di Milano": the first works but the second not, even though the title is that one. – ilariaroglieri Jun 23 '14 at 16:57
  • In case this helps, the taxonomy value is exactly "Politecnico di Milano". – ilariaroglieri Jun 23 '14 at 17:04