Custom query return

时间:2016-11-02 作者:RichieR

以下是我使用的完整代码:

<form action ="" method="get">
    <INPUT TYPE="checkbox" NAME="colour" VALUE="red"> Red<BR>
    <INPUT TYPE="checkbox" NAME="colour" VALUE="blue"> Blue<BR>
    <button type="submit" name="">Search</button>
</form>

<?php
    if ( $_GET[\'colour\'] && !empty ($_GET[\'colour\'] ) ) {
        $colours= $_GET[\'colour\']; 
    }

    $meta_query = array( \'relation\' => \'OR\' );
    foreach ( $colours as $colour ) {
            $meta_query[] = array(
                    \'key\'       => \'colour\',
                    \'value\'     => $colour, 
                    \'compare\'   => \'like\', 
            );
    }

    $query = new WP_Query( $meta_query );
    while ( $query->have_posts() ) : $query->the_post();
?>

1 个回复
SO网友:rudtek

好的,我将尝试添加此作为答案,因为我无法在注释中编写代码:

<?php
if($_GET[\'colour\'] && !empty ($_GET[\'colour\']))
{
$colours= $_GET[\'colour\'];  //notice i pluralized.
}
?>  //your code ended php, so i assume there is something here as it is not started again.


$meta_query = array(\'relation\' => \'OR\');
foreach ($colours as $colour) {
    $meta_query[] = array(
        \'key\'       => \'colour\',
        \'value\'     => $colour, //not sure why you have parentheses, but try with and without here i guess
        \'compare\'   => \'like\',  //switched back out of in because we are out of the array
    );
}