0

I have multiple taxonomies for a custom post type ('product') and I would like people to select from the list whether they want to include one or more of the terms and if they want to exclude one or more of the terms.

The form I've come up so far is:

<form method="post" name="en_tax" >
<?php $terms = get_terms( array(
  'taxonomy' => 'post_tag',
  'hide_empty' => false,
   ) );
 foreach ($terms as $terms){
   echo '<input type="radio" name="'.$terms->name.'"';
   if (isset($terms->name) && $terms->name=="yes") echo "checked";
   echo 'value="yes">With ';
   echo '<input type="radio" name="'.$terms->name.'"';
   if (isset($terms->name) && $terms->name=="no") echo "checked";
   echo 'value="no">Without '.$terms->name.'';
   echo '<br>';
 }
 $cat = get_terms( array(
  'taxonomy' => 'category',
  'hide_empty' => false,
  ) );
 foreach ($cat as $cat){
   echo '<input type="radio" name="'.$cat->name.'"';
   if (isset($cat->name) && $cat->name=="yes") echo "checked";
   echo 'value="yes">With ';
   echo '<input type="radio" name="'.$cat->name.'"';
   if (isset($cat->name) && $cat->name=="no") echo "checked";
   echo 'value="no">Without '.$cat->name.'';
   echo '<br>';
   }
  ?>
<input type="submit">
</form>

But I'm not sure how to put the results into a query like this with each no value being 'NOT IN' and each yes value being 'IN':

<?php $attachments = get_posts( array(
  'post_type'      => 'product',
  'posts_per_page' => -1,
  'tax_query' => array(
     'relation' => 'AND',
     array(
    'taxonomy' => 'post_tag',
    'field'    => 'slug',
    'terms'    => 'custom-tag',
    'operator' => 'NOT IN', 
    ),
   array(
     'taxonomy' => 'collection',
     'field'    => 'slug',
     'terms'    => 'custom-cat',
     )
    ),
  ) );

 if ( $attachments ) {
 foreach ( $attachments as $post ) {
     setup_postdata( $post );
     the_title();
     echo '<br>';

 }
 wp_reset_postdata();
 }?>
0

If I understood you correctly, an array of terms either slug or term_id, for example, depending on field to be exact.

$args = array(
    'post_type' => 'a_post_type',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'tax_one',
            'field'    => 'slug',
            'terms'    => array( 'action', 'comedy' ),
        ),
        array(
            'taxonomy' => 'tax_two',
            'field'    => 'term_id',
            'terms'    => array( 103, 115, 206 ),
            'operator' => 'NOT IN',
        ),
    ),
);

See Codex: WP_Query

For the form, in short, make name an array basically, e.g.:

<form method="post" name="en_tax" >
<input type="checkbox" name="post_variable[]" value="value 1">
<input type="checkbox" name="post_variable[]" value="value 2">
<input type="submit" />
</form>

You then can get the variable $_POST[ 'post_variable' ], which is an array of values.

  • That does help, but I'm looking for a way to grab the selections from the form and put them into query. – Kaye O'Malley Nov 18 '17 at 1:29

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.