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.
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
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]
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?
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.
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.
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.
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 π
Laveklint, any chance you could share your code?
Thx in advance
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
danzigism, could you post your example if possible?
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!
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
danzigism,
Could you please post your example?
Thank you.
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!