WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Search Form with Custom Taxonomy Dropdown (17 posts)

  1. danzigism
    Member
    Posted 3 years ago #

    Hey there guys. I am using a custom post type and a custom taxonomy for displaying business listings on a directory website I'm creating.

    I need the search feature of WordPress to search both a Keyword and dropdown menu category(taxonomy) selector as a part of the search query. I'm using a method for the dropdown posted here: http://wordpress.org/support/topic/terms-of-custom-taxonomy-in-a-dropdown-menu?replies=6

    Here's my searchform.php as of now:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    As you can see, the get_terms_dropdown() function is perfectly capable of giving me a full dropmenu of my taxonomy. I just need to figure out if it is possible to take the selection of the dropdown, and append it to the search query somehow in addition to their keyword.

    For example, if someone wants to search by zip code, they can type in the zip code in the text field, then select a category in the drop down like "computer services" and it will display all the listings in the computer services category with matching keywords.

    Just curious if anyone knows how I can take the value of the selector and make it a part of the query. Thanks for any help you can provide.

  2. danzigism
    Member
    Posted 3 years ago #

    I went ahead and added a value to the option selector in the function:

    function get_terms_dropdown($taxonomies, $args){
    	$myterms = get_terms($taxonomies, $args);
    	$output ="<select>";
    	foreach($myterms as $term){
    		$term_taxonomy=$term->Categories;
    		$term_slug=$term->slug;
    		$term_name =$term->name;
    		$link = $term_slug;
    		$output .="<option value='".$link."'>".$term_name."</option>";
    	}
    	$output .="</select>";
    return $output;
    }

    Doesn't seem to be appending anything to the query. Still only operates primarily when typing in a keyword in the text input.

  3. danzigism
    Member
    Posted 3 years ago #

    Ahh I'm getting close. I forgot to add the name="s" attribute to the <select> tag. It is added but maybe I'm thinking about this the wrong way. Now it just simply uses the slug of the taxnomy term as a keyword.

    The URL now looks like:

    http://www.mydomain.com/?s=KEYWORD&s=MYTAXTERM

    Perhaps I'm going about this the wrong way. How can I customize the query so that it specifically searches for keywords that match the selected taxonomy term?

    I think the URL needs to formed as such:

    http://www.mydomain.com/?taxonomy=TAXONOMY&term=TAXTERM&s=KEYWORD

  4. danzigism
    Member
    Posted 3 years ago #

    GOT IT WORKING!

    I specified the wrong name="" attribute for the <select> tag. It needs to be formed like this:

    <select name="taxonomy=TAXONOMY&term">

    I was just using "s" which just does a basic keyword search without a taxonomy specification. This now completely appends to the search. Here's the complete code for anyone who needs this kind of functionality.

    [Code moderated as per the Forum Rules. Please use the pastebin]

  5. danzigism
    Member
    Posted 3 years ago #

    ugh. so I thought I was done. However, this kind of search query does not work correctly.

    When using a URL such as:

    http://mydomain.com/?taxonomy=TAXONOMY&term=TAXTERM&s=KEYWORD

    It simply ignores the term and just displays the complete list of results based on the KEYWORD.

    Anybody know how to ensure that the results *only* display the items that are within that specific term?

  6. danzigism
    Member
    Posted 3 years ago #

    revised my search URL so that it just uses:

    http://mydomain.com/?taxterm=TAXTERM&s=KEYWORD

    this displays the right tax term, but the tax term gets overrided by the keyword.

  7. danzigism
    Member
    Posted 3 years ago #

    Ok.. After much frustration and hours worth of work, I finally got it all fixed. And of course it was easier than I thought. Let us keep in mind that when searching for keywords in a specific taxonomy, it is not related to the URL. What is important is the $_GET data.

    here's my searchform.php

    [Code moderated as per the Forum Rules. Please use the pastebin]

    and here's some custom code you can place in your main content area in the search.php page that I made using query_posts and the respective taxonomy array.

    [Code moderated as per the Forum Rules. Please use the pastebin]

    might be a little messy, but it works pretty well.

  8. Laveklint
    Member
    Posted 3 years ago #

    Hi Danzigism,

    I'm interested in your solution for this issue.. Your code got moderated unfortunately, would you mind posting it at pastebin or elsewhere?

    Thanks.

  9. Laveklint
    Member
    Posted 3 years ago #

    I got it working, thanks anyways. I'm have multiple taxonomies with multiple terms in each taxonomy. I can now do a search and combine the different taxonomies and terms :)

  10. salocined
    Member
    Posted 3 years ago #

    Laveklint, any chance you could share your code?
    Thx in advance

  11. Sadhas
    Member
    Posted 3 years ago #

    Hi, danzigism

    Maybe it will just help if someone can explain to me how the built in WordPress function works. But here's what I want:

    * Two search fields, by name and by location
    * The first field should check all the common post data (only posts) + categories (note: like ?s=xxx normarl search)
    * The second field should only check on the taxonomy filed
    * Only one field needs to be filled

    If it is possible, can you please give me a hint on where to start?

    i want combine result in wordpress

  12. salocined
    Member
    Posted 3 years ago #

    danzigism, could you post your example if possible?

  13. danzigism
    Member
    Posted 3 years ago #

    Hey there Sadhas, yes here's what you need to do:

    On your second (location) form field, you need to specify the "name" to be the slug name of your taxonomy term as a variable. For example I created a loop in my searchform.php that grabs *all* my tax terms and displays them in a drop-down as such.

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

    Then you'll need to modify your search.php template and make sure it grabs that new name value and queries the posts that have that tax term.

    <?php
    global $query_string;
    $termslug = $_GET['optionname'];
    ?>
    
    <h2>Search Results For: <span style="font-weight: normal"><em><?php the_search_query(); ?></em></span></h2>
    
    <?php 
    
    if (empty($termslug)) {
    	query_posts($query_string);
    	if(have_posts()) : while(have_posts()) : the_post();
    	endwhile; else:
    	_e('Sorry, no listings matched your criteria or you forgot select a category.');
    	endif;
    } 
    
    if (!empty($termslug)) {
             query_posts(array('YOURTAXONOMY' => $termslug) );
    	if(have_posts()) : while(have_posts()) : the_post();
    	endwhile; else:
    	_e('Sorry, no listings matched your criteria or you forgot select a category.');
    	endif;
    }
    
    ?>

    The first IF statement is for people that do not type in or select a "Location" or whatever your taxnomy term is. It will bypass that selection and query *all* posts based on whatever the user searched for.

    The second IF statement will search for whatever the user typed in AND whatever tax term (location) they selected. I hope this helps and doesn't get moderated. Good luck!

  14. Sadhas
    Member
    Posted 3 years ago #

    Sorry for Delay danzigism,

    Thanks you for your help. i have one more doubt danzigism. how create search.php and search template for custom post. Becoz now i developd for Job portal.i need search only for particular custom post type. Thanks for help

  15. nikita220022
    Member
    Posted 3 years ago #

    danzigism,
    Could you please post your example?
    Thank you.

  16. larsemil
    Member
    Posted 2 years ago #

    I solved this by doing this:

    This is my $_GET

    Array
    (
        [utbildningsniva] => Array
            (
                [0] => 12
            )
    
        [ort] => Array
            (
                [0] => 7
            )
    
        [s] => program
        [post_type] => Page
    )

    And then i did this in search.php

    <?php
    $search['post_type'] = 'page';
    $search['tax_query'] = array(
        'relation' => 'or',
    );
    foreach($_GET as $key => $option)
    {
        if(is_array($option))
        {
            array_push($search['tax_query'], array(
    	    'taxonomy' => $key,
    	    'field' => 'id',
    	    'terms' => $option
    	));
        }
    }
    
    query_posts($search);
    ?>

    Also note that in my case i have multiple terms and the user got the ability to choose multiple terms. Which means that i use lots of arrays in my search function.

    Good luck!

  17. dados
    Member
    Posted 2 years ago #

    "danzigism" can you please help me I need same thing like you.. I want to search by custom post and Custom Taxonomy Dropdown... And I need same thing like you describe:

    "For example, if someone wants to search by zip code, they can type in the zip code in the text field, then select a category in the drop down like "computer services" and it will display all the listings in the computer services category with matching keywords."

    Your code is moderate and I can not see it.. Can you please help me.. Please

Topic Closed

This topic has been closed to new replies.

About this Topic