如何形成具有多个参数的URL查询数组

时间:2019-01-20 10:20:46

标签: php sql arrays wordpress url-parameters

这是我正在制作的过滤器的第二页,用户可以在第一页上选中复选框。复选框的值通过URL中的参数传递到第二页: filter-result/?mytaxonomy=myterm&mytaxonomy=myotherterm 如何形成此数据的数组以用于(WP)查询?

我可以通过以下方式显示URL中的数据:

    if( isset( $_GET['mytaxonomy'] ) ){
        foreach( $_GET['mytaxonomy'] as $term ){
            echo $term . '<br>';
        }
    }

我还可以查询帖子(custompost类型):

    $query = new WP_Query( array(
        'post_type' => 'mycustomposttype',
        'tax_query' => array(
            array(
                'taxonomy' => 'mytaxonomy',
                'field' => 'slug',
                'terms' => array( 'myterm', 'myotherterm' ),
                'operator' => 'AND',
            ),
        ),
    ) );

我想将数据从$_GET['mytaxonomy']传递到'terms' => array( *inside here* )

当我使用print_r ($_GET['mytaxonomy']);时,结果为Array ( [0] => myterm ),一切正确。我想我只需要将数组形成为'a', 'b'即可在WP查询中使用。我该如何实现?

2 个答案:

答案 0 :(得分:0)

您可以像filter-result/?mytaxonomy[]=myterm&mytaxonomy[]=myotherterm一样将数组传递给php-您使用的语法来自Java世界(不适用于php)

答案 1 :(得分:0)

对于其他可能会质疑如何解决此问题的人:这很容易解决。因为正如@Wodka所建议的那样,我使用[]括号形成了链接,例如:<input type="checkbox" name="mytaxonomy[]" value="myterm">,并且由于$_GET['mytaxonomy']本身输出了array(),因此我可以像下面这样将其放入。< / p>

'terms' => $_GET['mytaxonomy'],

导致的结果:

$query = new WP_Query( array(
    'post_type' => 'mycustomposttype',
    'tax_query' => array(
        array(
            'taxonomy' => 'mytaxonomy',
            'field' => 'slug',
            'terms' => $_GET['mytaxonomy'],
            'operator' => 'AND',
        ),
    ),
) );