0

So I have 8 checkboxes: A,B,C,D,E,F,G,H for example. All of them have the same name, and I need to get their values as an array in meta_query. I hope code can explain better than I do :)

$args = array(
  'post_type'=>'paibcresume',
  'posts_per_page' => 10,
  'paged' => $paged,
  'meta_query' => array(),
  'tax_query' => array(),
  'orderby' => 'date',
  'meta_key' => '',
  'order' => 'DESC'
);

// Search by occupation
//rbwwbusy - custom field
//rbseroccupation - search checkbox fields
if (isset($_GET['rbseroccupation']) && !empty($_GET['rbseroccupation'])) {
  $args['meta_query'][] = array(
    'key'     => 'rbwwbusy',
    'value'   => array($_GET['rbseroccupation']),
    'compare' => 'IN'
  );
}

<div class="occupation">
  Occupation:<br>
  <input type="checkbox" name="rbseroccupation" value="A">&nbsp;A
  <input type="checkbox" name="rbseroccupation" value="B">&nbsp;B
  <input type="checkbox" name="rbseroccupation" value="C">&nbsp;C
  <input type="checkbox" name="rbseroccupation" value="D">&nbsp;D<br>
  <input type="checkbox" name="rbseroccupation" value="E">&nbsp;E
  <input type="checkbox" name="rbseroccupation" value="F">&nbsp;F
  <input type="checkbox" name="rbseroccupation" value="G">&nbsp;G
  <input type="checkbox" name="rbseroccupation" value="H">&nbsp;H
</div>

So my question is: it works, but it only displays results for the last checked checkbox. So for example if i look for A and B, it only displays B; if I look for A, B and C it only display posts with C.

What do I need to do, so it shows for example posts with A,B and C, not only the last checked?

Thank you!

1 Answer 1

1

You have to put the input names like this

<div class="occupation">
  Occupation:<br>
  <input type="checkbox" name="rbseroccupation[]" value="A">&nbsp;A
  <input type="checkbox" name="rbseroccupation[]" value="B">&nbsp;B
  <input type="checkbox" name="rbseroccupation[]" value="C">&nbsp;C
  <input type="checkbox" name="rbseroccupation[]" value="D">&nbsp;D<br>
  <input type="checkbox" name="rbseroccupation[]" value="E">&nbsp;E
  <input type="checkbox" name="rbseroccupation[]" value="F">&nbsp;F
  <input type="checkbox" name="rbseroccupation[]" value="G">&nbsp;G
  <input type="checkbox" name="rbseroccupation[]" value="H">&nbsp;H
</div>

The rest of the code should work fine.

Edit: You should change this

 'value'   => array($_GET['rbseroccupation']) 

to

 'value'   => $_GET['rbseroccupation']

or better to include the case where nothing is selected

 'value'   => (isset($_GET['rbseroccupation'])?$_GET['rbseroccupation']:array())

Because adding [] to input name makes it an array by deafult

Sign up to request clarification or add additional context in comments.

1 Comment

Definitily missed that part, thank you very much! Maybe i shouldn't work that late at night :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.