Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

search in multiple custom taxonomy WordPress

  • SOLVED

Hi! This is my situation:

- I have a custom posts types called "schedahotel"
- One custom taxonomy called "localita_hotel"
- One custom taxonomy called "fasciadiprezzo"

I want a search page with a custom searchform, for searching around my custom posts types called "schedahotel" for example:

<strong>- A dropdown list for my taxonomy called "localita_hotel" (i exclude manually certain ID)
- A dropdown list for my taxonomy called "localita_hotel" (i exclude manually certain ID)</strong>
- A dropdown list fro my taxonomy called "localita_hotel"
- Submit button

All select are independent and not required.

Many Thanks!

View the image...

Answers (4)

2011-11-25

Luis Abarca answers:

I did something similar here, let me search for the code

- [[LINK href="http://ihata.com.mx/"]]www.ihata.com.mx[[/LINK]]
- [[LINK href="http://mchome.com.mx/propiedades/"]]mchome.com.mx/propiedades/[[/LINK]]

1.- Put name to each dropdown
2.- filter with tax_query


Luis Abarca comments:

// function.php or your plugin

// use it in your templates to show a dropdown
function my_dropdown($name, $taxonomy = 'category')
{
$defaults = array(
'taxonomy' => $taxonomy,
'id' => $name,
'name' => $name,
'show_option_none' => ' - Select - ',
'selected' => get_query_var($name)
);

wp_dropdown_categories($defaults);
}


add_action('pre_get_posts', 'my_customsearch');

/**
*
*/
function my_customsearch()
{
global $wp_query;

if ( $wp_query->query_vars['post_type'] != 'schedahotel' ||
!$wp_query->is_post_type_archive ) {
return false;
}

$tax_query = array();
$operacion = get_query_var('operacion');
$ubicacion = get_query_var('ubicacion');
$precio = get_query_var('precio');

// first dropdown
if (!empty($operacion) && $operacion > 0) {
$tax_query[] = array(
'taxonomy' => 'localita_hotel',
'field' => 'id',
'terms' => $operacion
);
}

// locations
if (!empty($ubicacion) && $ubicacion > 0) {
$tax_query[] = array(
'taxonomy' => 'localita_hotel',
'field' => 'id',
'terms' => $ubicacion
);
}

// price
if (!empty($precio) && $precio > 0) {
$tax_query[] = array(
'taxonomy' => 'localita_hotel',
'field' => 'id',
'terms' => $precio
);
}

if ( sizeof($tax_query) > 0 ) {
$tax_query['relation'] = 'AND';

$wp_query->query_vars['tax_query'] = $tax_query;
}

return false;
}

// }}}
// {{{

// add my custom query vars
add_filter('query_vars', 'mycustom_query_vars');

function mycustom_query_vars($query_vars)
{
$query_vars[] = 'operacion';
$query_vars[] = 'ubicacion';
$query_vars[] = 'precio';

return $query_vars;
}


christian pucci comments:

thanks Luis, how can i display that on my search.php?


Luis Abarca comments:


function my_customsearch()
{
global $wp_query;

<blockquote>// change this
if ( $wp_query->query_vars['post_type'] != 'schedahotel' ||
!$wp_query->is_post_type_archive ) {
return false;
}</blockquote>


<strong>// to this just change the values in search
if ( !$wp_query->is_search ) {
return false;
}</strong>
...


christian pucci comments:

Sorry Luis, i have a custom page called "Preventivo Hotel"...


Luis Abarca comments:

OK, then change the "if" statement to check if you are in that page.


$page = get_page_by_path('/preventivo-hotel');

if ( !is_page($page->ID) ) {
return false;
}
...


christian pucci comments:

Luis, i have now tested your code:

i've put that on my functions.php http://pastebin.com/4zXPN2nw

and this is the code for the custom form http://pastebin.com/MyL9rYSq

my result in my page test: http://d.pr/CX9


Luis Abarca comments:

You need to add a name to each dropdown in your form


<?php my_dropdown('hotel', 'localita_hotel'); // repeat foreach dropdown ?>
<?php my_dropdown('hotel-category', 'localita_hotel_category'); // use your own taxonomies ?>


http://pastebin.com/LVgthfnt

Then, register the query vars to match your form fields


add_filter('query_vars', 'mycustom_query_vars');

function mycustom_query_vars($query_vars)
{
$query_vars[] = 'hotel';
$query_vars[] = 'hotel-category';
....

return $query_vars;
}

2011-11-25

Arnav Joy answers:

Please explain more since both of the drop downs you mentioned here showing the same taxonomy while there are two different taxonomies are there and second thing is that your image attachment image is different then what you need,please expain.

use this plugin may be this will help you-

http://wordpress.org/extend/plugins/taxonomy-picker/

otherwise I am there to help you.

thanks
Robin

2011-11-25

Romel Apuya answers:

are you open for using a plugin?
this one might be the one you needed.

[[LINK href="http://wordpress.org/extend/plugins/query-multiple-taxonomies/"]]http://wordpress.org/extend/plugins/query-multiple-taxonomies/[[/LINK]]

it needs customization though..

2011-11-25

Daniele Raimondi answers:

Hello Christian, I've made something similir on this site:

[[LINK href="http://www.real-dreams.it"]]http://www.real-dreams.it[[/LINK]]

The form on the right search posts in a CPT using a custom taxonomy (boat or cars) and custom fields (min price, max price).

[[LINK href="http://pastebin.com/Jctkjajk"]]here [[/LINK]]you can find the code for the form, and [[LINK href="http://pastebin.com/BJ1vu2WS"]]here [[/LINK]]the code for the search-results page.

Ciao!


christian pucci comments:

Ciao! Con dropdown?


Daniele Raimondi comments:

If you want to use a dropdown with all terms for a particular taxonomy, just fill the SELECT with the results of a query like this (in this example, cars_features is my taxonomy name and I'm creating a list of its terms):

<?php
//get siblings of this term
$cars_features=( get_terms(
'car_features',
array(
'taxonomy' => 'car_features',
'parent' => 0,
'hide_empty' => true,
'number' => $sb_max_number_of_terms,
)
));
?>
<?php if ( count($cars_features) ) : ?>
<div class="modultimiannunci">
<div class="contenttitlemodule">
<h2>Caratteristiche</h2>
<h3>autovetture</h3>
</div>
<ul>
<?php foreach ($cars_features as $cars_feature) : ?>
<li><a href="<?php echo get_term_link($cars_feature->slug,$cars_feature->taxonomy)?>"><?php echo $cars_feature->name; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif;?>


the code for the search query remain the same. If you have more than 1 taxonomy to query for just use syntax explained [[LINK href="http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters"]]here[[/LINK]] on <strong>Multiple Taxonomy Handling</strong>