简体   繁体   English

WordPress:wp_query,meta_query数组

[英]WordPress: wp_query, meta_query array

So I have 8 checkboxes: A,B,C,D,E,F,G,H for example. 因此,我有8个复选框:例如A,B,C,D,E,F,G,H。 All of them have the same name, and I need to get their values as an array in meta_query . 它们都具有相同的名称,我需要在meta_query它们的值作为数组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; 因此,例如,如果我寻找A和B,则只显示B; if I look for A, B and C it only display posts with C. 如果我寻找A,B和C,则只会显示带有C的帖子。

What do I need to do, so it shows for example posts with A,B and C, not only the last checked? 我需要做什么,例如显示带有A,B和C的帖子,而不仅仅是最后检查的帖子?

Thank you! 谢谢!

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 因为在输入名称中添加[]使其成为默认数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM