WordPress.org

Ready to get started?Download WordPress

Forums

best way to search for taxonomy terms (9 posts)

  1. ivanoats
    Member
    Posted 3 years ago #

    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. http://dev.greendegreedirectory.com, 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.

    Thanks,
    Ivan

  2. ivanoats
    Member
    Posted 3 years ago #

    for example, here is some WP_Query code that is not returning the results I am looking for:

    $result_posts = new WP_Query();
    $result_posts->query('showposts=5&post_type=gddprogram&country=United+Kingdom');
  3. scribu
    Member
    Posted 3 years ago #

    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:

    $result_posts->query('showposts=5&post_type=gddprogram&country=united-kingdom');

    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.

  4. ivanoats
    Member
    Posted 3 years ago #

    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
    unset($search_query['pagename']);
    $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:
    http://gdd.local/degree-programs?country=United+Kingdom
    I get the 404 error page instead of my code

    any idea where I should look?

  5. scribu
    Member
    Posted 3 years ago #

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

    How are you defining the Country taxonomy?

  6. danzigism
    Member
    Posted 3 years ago #

    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:

    http://mydomain.com/?movies=horror&s=spongebob

    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?

  7. scribu
    Member
    Posted 3 years ago #

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

  8. danzigism
    Member
    Posted 3 years ago #

    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" /> 
    
    <?php
    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_taxonomy=$term->My_Taxnomy;
    		$term_slug=$term->slug;
    		$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" />
    </form>

    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?

  9. danzigism
    Member
    Posted 3 years ago #

    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.

    http://wordpress.org/support/topic/search-form-with-custom-taxonomy-dropdown?replies=7#post-1916364

Topic Closed

This topic has been closed to new replies.

About this Topic