admin管理员组

文章数量:1298449

I have the following WP Query:

if(isset($_GET['scope']) && !empty($_GET['scope'])) {
    $scope = $_GET['scope'];
} else {
    $scope = '';
}

$foo = new WP_Query([
    'post_type' => 'people',
    'posts_per_page' => -1,
    'meta_query' => [
        [
            'key' => 'name',
            'value' => $scope,
            'compare' => '='
        ]
    ]
]);

And this dropdown menu:

<form class="criteria-form" action="<?=site_url('/page..')?>">
    <select name="scope" class="criteria-select" onchange="this.form.submit()">
        <option value="" <?= $scope  ? 'selected' : '' ?>>All Foo</option>
        <option value="foo-red" <?= $scope == 'Foo Red' ? 'selected' : '' ?>>Foo Red</option>
        <option value="Foo-green" <?= $scope == 'Foo Green' ? 'selected' : '' ?>>Foo Green</option>
    </select>
</form>

All is working good for options Foo Red and Foo green because it returns specified values, but how can I select all values using the same compare operator? What I want to do is when user selects All foo he gets Red and Green. If I use LIKE operator instead of = I get Red and Green Foos mixed.

Maybe there is some better way to change operator for that specific select option and get all results.

I have the following WP Query:

if(isset($_GET['scope']) && !empty($_GET['scope'])) {
    $scope = $_GET['scope'];
} else {
    $scope = '';
}

$foo = new WP_Query([
    'post_type' => 'people',
    'posts_per_page' => -1,
    'meta_query' => [
        [
            'key' => 'name',
            'value' => $scope,
            'compare' => '='
        ]
    ]
]);

And this dropdown menu:

<form class="criteria-form" action="<?=site_url('/page..')?>">
    <select name="scope" class="criteria-select" onchange="this.form.submit()">
        <option value="" <?= $scope  ? 'selected' : '' ?>>All Foo</option>
        <option value="foo-red" <?= $scope == 'Foo Red' ? 'selected' : '' ?>>Foo Red</option>
        <option value="Foo-green" <?= $scope == 'Foo Green' ? 'selected' : '' ?>>Foo Green</option>
    </select>
</form>

All is working good for options Foo Red and Foo green because it returns specified values, but how can I select all values using the same compare operator? What I want to do is when user selects All foo he gets Red and Green. If I use LIKE operator instead of = I get Red and Green Foos mixed.

Maybe there is some better way to change operator for that specific select option and get all results.

Share Improve this question asked Aug 12, 2022 at 8:08 MaaverickMaaverick 51 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You can put your meta_query in a variable to achieve the desired result. Then you can fill the variable with your actual meta_query when your condition is met. Check out the code below.

$meta_query = array();

if ( isset($_GET['scope']) && !empty($_GET['scope']) ) {

    $meta_query[] = array(
        'key'     => 'name',
        'value'   => $_GET['scope'],
        'compare' => '='
    );

}

$foo = new WP_Query([
    'post_type' => 'people',
    'posts_per_page' => -1,
    'meta_query' => array(
        $meta_query
    )
]);

本文标签: phpWPQuery How to get results from both metakey options