首页 文章

Wordpress,PHP,Meta Query传递get / post变量

提问于
浏览
0

编辑:我重命名所有字段,以便名称!= key []现在名称=池或漩涡,我一次只做一个....

所以我可以让它工作:

$args = array(
'numberposts'    => -1,
'category_name' => 'reviews',
'post_status'       => 'publish',
'meta_query'        =>  array(
    'relation'  => 'AND',
    array(
        'key'       => 'whirlpool',
        'value'     => '',
        'compare'   => '!='
    ),
 array(
        'key'       => 'pool',
        'value'     => '',
        'compare'   => '!='
    )
)
); 

$the_query = new WP_Query($args);

但我不能让arraypush工作或这个:

$query_array = array('relation' => 'AND');


if(isset($_GET['massage']) && !empty($_GET['massage'])){
$massage = $_GET['massage'];
array_push($query_array, array('key' => 'massage','value' => '','compare'=> '!='));
}
if(isset($_GET['facial']) && !empty($_GET['facial'])){
$facial = $_GET['facial'];
array_push($query_array, array('key' => 'facial','value' => '','compare'=> '!='));
}

if(isset($_GET['manicure']) && !empty($_GET['manicure'])){
$manicure = $_GET['manicure'];
array_push($query_array, array('key' => 'manicure','value' => '','compare'=> '!='));
}

 $the_query = new WP_Query( array( 
                'numberposts' => -1,
                'category_name' => 'reviews',
                'post_status'       => 'publish',
                'query' => $query_array ) 
            );

我觉得我已经把这脑子弄清楚了,但我似乎无法将其击败 . 请不要担心消毒,什么不是...... -

我想从复选框中发布变量,然后在它们上绘制以通过元键搜索特定的帖子(只要它存在为某些东西,值就不重要.. isset()我想 .

HTML

<li><input type="checkbox" name="key[]" value="pool" id="pool"><label class="checkbox" for="pool"> Pool </label></li>

<li><input type="checkbox" name="key[]" value="whirlpool" id="whirlpool"><label class="checkbox" for="whirlpool"> Whirlpool </label></li>

期望的PHP

$args = array(
'numberposts' => -1,
'category_name' => 'reviews',      
'meta_query' => array(
    'relation' => 'AND',
    array(
        'key' => 'pool',
    ),
    array(
        'key' => 'whirlpool',
    )
)

);

我可以简单地将一个已清理的$ _GET ['key']传递给像这样的数组 .

array(
        'key' => '$_GET['key']['0']',
    ),
    array(
        'key' => '$_GET['key']['1']',
    )

如果是这样,我不完全确定如何将foreach循环与WP_Query args结合使用 .

我提前为此格式化道歉 .

1 回答

  • 0

    试试这个 -

    $arr = array('relation' => 'AND');
    
    foreach($_GET['key'] as $key){
      $arr[] = array('key' => $key);
    }
    

    然后在元查询中使用它 -

    $args = array(
    'numberposts' => -1,
    'category_name' => 'reviews',      
    'meta_query' => $arr
    );
    

相关问题