0

I have this structure:

taxonomy
    - term1
    -- child term1
    -- child term2

    -term2
    -- child term1
    -- child term2

I have two select boxes. First one shows the parent terms (term1 and term2) and the second one shows the childs of the selected parent.

I submit the form to "search-results.php". How can I get the values of both selects to create this kind of query?

$args = array(
   'tax_query' => array(                        
       array(
          'taxonomy' => 'state',
          'field' => 'slug',
          'terms' => array( term1, childTerm2 ),
          'operator' => 'IN'
       )
    )
);

Thanks.

0

It all depends on how you're trying to do it. When it comes to filtering results, i usually setup the form to use $_GET and filter the results when the page loads again:

if (isset($_GET['term'])) {
    $term = $_GET['term'];
} else {
    $term = 'defaultTerm';
}

if (isset($_GET['childterm'])) {
    $childterm = $_GET['childterm'];
} else {
    $childterm = 'defaultChild';
}

$args = array(
   'tax_query' => array(                        
       array(
          'taxonomy' => 'state',
          'field' => 'slug',
          'terms' => array( $term, $childterm ),
          'operator' => 'IN'
       )
    )
);
  • Thanks for your answer, but... if $childterm is null because de user has not selected nothing in the second select box? – rec Jun 10 '12 at 21:25
  • you can use $childterm = '' instead of 'defaultChild' for that, so that the query will bypass the empty positions on the array. – moraleida Jun 10 '12 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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