Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So I got a custom search form with three dropdowns containing three different custom taxonomies.

The problem is, I'd need the taxonomy slugs in the address for the search to fuction properly, because now I only get the IDs. If I have product category with the name/slug of 50, when searching it outputs the taxonomy ID which is 6 in the address.

Like so:

/?s=&product_category=6&product_maksuaika=14&product_ikaraja=0

When it should be:

/?s=&product_category=50&product_maksuaika=14&product_ikaraja=0

Same goes for all of the three taxonomies I have.

Here's my function:

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->is_search ) {
    return false;
  }

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

  // first dropdown

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

  // second dropdown

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

  // third dropdown

  if (!empty($precio) && $precio > 0) {
      $tax_query[] = array(
        'taxonomy' => 'product_maksuaika',
        'field'    => 'id',
        'terms'    => $ubicacion
      );

  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; 
 }

Simply changing the field to slugs or names in the array does not do the trick.

Any ideas?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Nothing stands out as 'wrong' - though you do have a few errors:

  • The third tax_query conditional is missing a closing bracket
  • The third tax_query should be querying precio not $ubicacion.
  • Cast the ID variables as integers

I'd suggest putting WordPress into debug mode to highlight any syntax errors. Try doing it for only one taxonomy first, and then add the others in. If using WP3.3+ I would also suggest the following set up:

add_action('pre_get_posts', 'my_customsearch'); 
function my_customsearch($query) {   

    if(! $query->is_main_query() || ! $query->is_search )
       return;

    $tax_query = array();
    $operacion = (int) get_query_var('product_category');

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

   if ( sizeof($tax_query) > 0 ) {
      $tax_query['relation'] = 'AND';
      $query->set( 'tax_query', $tax_query );
   }

}

Keep in mind this will replace any tax query that is part of the main search.

share|improve this answer
    
Thank you for pointing out the errors, guess that's what you get when you work with something for too long. The search works like it should now with your code so thank you very much. –  Andrew Apr 10 '12 at 9:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.