Skip to main content
added 72 characters in body; edited tags
Source Link
Stephen Harris
  • 32.6k
  • 6
  • 84
  • 118
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; 
 }
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; }
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; 
 }
Source Link
Andrew
  • 85
  • 1
  • 9

taxonomy query to output slugs instead IDs

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?