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.

I am trying to create a Search Filter within WordPress to filter through all the custom taxonomies within a Custom Post Type.

I have created a function that generates the dropdown so I can output each option value as a slug for my filter which is working already. My only issue is that the "Select All" option is not working.

The results come back with nothing found when Selecting All. The filter can be here.

The code I have used to create the function is:

function adopt_custom_taxonomy_dropdown( $taxonomy, $orderby = 'date', $order = 'DESC', $limit = '-1', $name, $show_option_all = null, $show_option_none = null ) {
$args = array(
    'orderby' => $orderby,
    'order' => $order,
    'number' => $limit,

);
$terms = get_terms( $taxonomy, $args );
$name = ( $name ) ? $name : $taxonomy;
if ( $terms ) {
    printf( '<select name="%s" class="postform">', esc_attr( $name ) );
    if ( $show_option_all ) {
        printf( '<option value="0">%s</option>', esc_html( $show_option_all ) );
    }
    if ( $show_option_none ) {
        printf( '<option value="-1">%s</option>', esc_html( $show_option_none ) );
    }
    foreach ( $terms as $term ) {
        printf( '<option value="%s">%s</option>', esc_attr( $term->slug ), esc_html( $term->name ) );
    }
    print( '</select>' );
}
}

and this is where the results are being pulled to...

<?php
        if (isset($_GET["farm-type"]) && empty($_GET["location-farms"])){

        $farm_type = $_GET["farm-type"];

        $myquery1['tax_query'] = array(
            array(
                'taxonomy' => 'farm-type',
                'terms' => array($farm_type),
                'field' => 'slug',

            ),
        );
        query_posts($myquery1);

        }
        ?>

        <?php
        if (isset($_GET["farm-type"]) && isset($_GET["location-farms"])){

        $farm_type = $_GET["farm-type"];
        $farm_location = $_GET["location-farms"];

        $myquery2['tax_query'] = array(
            array(
                'taxonomy' => 'farm-type',
                'terms' => array($farm_type),
                'field' => 'slug',
            ),
            array(
                'taxonomy' => 'location-farms',
                'terms' => array($farm_location),
                'field' => 'slug',
            ),
        );
        query_posts($myquery2);

        }
        ?>
share|improve this question
    
What processes the search? –  s_ha_dum May 24 at 18:52
    
Added extra code to show where the search is being pulled to @s_ha_dum –  damienoneill2001 May 26 at 7:39

1 Answer 1

Couple of notes:

  1. Your form action is incorrect: <form method="get" action="http://www.adoptapet.ie/archive-farm/">. The page is <form method="get" action="http://www.adoptapet.ie/archive-farms/">
  2. You should reorder your argument list for adopt_custom_taxonomy_dropdown(). As written, most of your arguments have default values and can be omitted when the function is called, except that as written argument five doesn't have a default so you can't in practice omit two, three, and four. Move $name to the front-- much better usability.

    adopt_custom_taxonomy_dropdown( $taxonomy, $name, $orderby = 'date', $order = 'DESC', $limit = '-1', $show_option_all = null, $show_option_none = null )
    
  3. Don't use query_posts(), ever. It clobbers the main query and can cause numerous problems.
  4. While I have not edited the code into the suggestions below, you should not ever use user supplied data like $_GET without sanitizing it. $_GET and $_POST are not safe.

I think your problem is mostly a logic issue. This...

isset($_GET["farm-type"]) && isset($_GET["location-farms"])

.... is always going to be true. $_GET["farm-type"] and $_GET["location-farms"] are always going to be set when your form submits, even if they are empty. You should be using !empty() instead.

And you can clean up this code a lot:

if (!empty($_GET["farm-type"])){
  $farm_type = $_GET["farm-type"];
  $myquery['tax_query'][] = 
    array(
      'taxonomy' => 'farm-type',
      'terms' => array($farm_type),
      'field' => 'slug',
    );
}

if (!empty($_GET["location-farms"])){
  $farm_location = $_GET["location-farms"];
  $myquery['tax_query'][] = 
    array(
      'taxonomy' => 'location-farms',
      'terms' => array($farm_location),
      'field' => 'slug',
    );
}

if (!empty($myquery)) {
  $q = new WP_Query($myquery); // instead of wp_query()
  var_dump($q);
}

And you probably want an AND relationship between your two components.

if (!empty($myquery)) {
  if (1 < count($myquery['tax_query'])) {
    $myquery['tax_query']['relation'] = 'AND';
  }
  $q = new WP_Query($myquery);
  var_dump($q->request); // debug
}

Now, if you pass an empty string for your "Select All" option instead of a number, I believe you should get the results you are looking for.

share|improve this answer
    
Thank you @s_ha_dum for your very very detailed answer, I have a lot to learn! I fixed point 1. I amended point 2 but it seemed to try search by the date instead of the taxonomy so I swapped it back for now. I have also added the code you then suggested however when I search now for All, no results come back and when I try some of the options, I get a lot of code displaying on the results page! I also removed the number from the value in this line printf( '<option value="">%s</option>', esc_html( $show_option_all ) ) What have I done wrong on this? :) adoptapet.ie/pets/farms. –  damienoneill2001 May 26 at 15:50

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.