WordPress:wp_query,meta_query数组

时间:2014-09-05 19:26:56

标签: php arrays wordpress get wp-query

所以我有8个复选框:例如A,B,C,D,E,F,G,H。它们都具有相同的名称,我需要在meta_query中将它们的值作为数组。我希望代码可以比我更好地解释:)

$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>

所以我的问题是:它有效,但它只显示最后一次选中复选框的结果。因此,例如,如果我寻找A和B,它只显示B;如果我查找A,B和C,它只显示带有C的帖子。

我需要做什么,所以它显示了例如A,B和C的帖子,而不仅仅是最后一次检查?

谢谢!

1 个答案:

答案 0 :(得分:1)

您必须输入这样的输入名称

<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>

其余代码应该可以正常工作。

编辑: 你应该改变这个

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

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

或更好地包括未选择任何内容的情况

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

因为将[]添加到输入名称会使其成为deafult的数组