Support » Fixing WordPress » best way to search for taxonomy terms

  • Hi, I could use some help/pointers on how to do a taxonomy term search page. A couple of issues I am having:

    1) Searching for terms with spaces in them does not appear to work.
    e.g., the search form on the left of the home page. search for United Kingdom in country does not work but searching for Canada does.

    2) How do I best write code that searches for partial words in taxonomy item names?
    e.g. searching for United should return posts with the term United States and United Kingdom.


Viewing 8 replies - 1 through 8 (of 8 total)
  • for example, here is some WP_Query code that is not returning the results I am looking for:

    $result_posts = new WP_Query();

    1) Searching for terms with spaces in them does not appear to work.

    That’s because WordPress usually uses term slugs for queries, which don’t contain spaces.

    So, the correct query would be:


    2) How do I best write code that searches for partial words in taxonomy item names?

    By using SQL LIKE. I think you will find the Search Everything plugin useful as a starting point.

    The easy way would be to add the built in autosuggest.js on each field of the form (the one you see when entering tags). Thus, you wouldn’t need partial name search.

    I have this code, which should work, but there seems to be something that I did not write (either in WordPress core or elsewhere, which is catching it first?)

    for example, I have a page called /degree-programs that uses a page template with the code below:

    global $query_string;
    $query_args = explode("&", $query_string);
    $search_query = array();
    foreach($query_args as $key => $string) {
        $query_split = explode("=", $string);
        # convert spaces to dashes
        $query_split[1] = preg_replace("/\s/","-",strtolower($query_split[1]));
        $search_query[$query_split[0]] = $query_split[1];
    } // foreach
    # remove pagename from query args
    $search = new WP_Query($search_query);
    # this works fine
    # $search = new WP_Query('showposts=5&post_type=gddprogram&country=United-Kingdom');

    but, if I browse to:
    I get the 404 error page instead of my code

    any idea where I should look?

    That’s because ‘country’ is registered as a taxonomy query var.

    How are you defining the Country taxonomy?

    I kind of have a similar issue except I’m trying to search for both keywords AND a user selected taxonomy term.

    for example the search URL should look something like this:

    Which should produce *no results* since spongebob is not a Horror flick. The query simply disregards the tax term specified in this case the movie genre and just shows ALL results for Spongebob.

    is there a special way to form these URLs when also including a specific tax term?

    No, it’s not related to the URL, but with how the WP_Query class simply ignores all parameters when a search is involved.

    Here’s what I have for my searchform.php. I have created a little function that grabs all the tax terms for the “My_Taxonomy” taxonomy and displays them in a drop down.

    <form role="search" method="get" id="searchform" action="<?php bloginfo('home'); ?>">
    <input type="text" value="" name="s" id="s" /> 
    function get_terms_dropdown($taxonomies, $args){
    	$myterms = get_terms($taxonomies, $args);
    	$selected = "selected";
    	$output ="<select><option selected='".$selected."'>Select a Category</option>'";
    	foreach($myterms as $term){
    		$term_name =$term->name;
    		$output .="<option name='".$term_slug."'>".$term_name."</option>";
    	$output .="</select>";
    return $output;
    $taxonomies = array('My_Taxonomy');
    $args = array('order'=>'ASC','hide_empty'=>true);
    echo get_terms_dropdown($taxonomies, $args);
    <input type="submit" id="searchsubmit" value="Search" />

    it obviously passes the ?s= portion of the URL, and at one point I had it form the URL as such: /?s=searchterm&taxonomy=taxterm but as you said the URL is not related.

    So how exactly could I take the values of the dropdown term selector, and make it a part of the search (search.php) results? Would I remove the current loop that I have in search.php and use WP_Query(); instead?

    I was able to resolve my particular issue by creating a custom search.php that uses the $_GET data from searchform.php to build a special query_posts(); function.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘best way to search for taxonomy terms’ is closed to new replies.