admin管理员组

文章数量:1122846

I have the following code which I think should work but obviously i'm missing something. Anyone who can tell why this is not correct?

//EDIT Here is what needs to happen. 'Merk', 'Type' and 'Dummy' are custom fields which are attributes for the product (not woocommerce).

I want to find all products that match either the 'Dummy' attribute false AND 'Merk' and 'Type' match the user input.

-- OR where 'Dummy' is true (AND another field 'Weight' will be matched, but firstly this should work also).

The new WP_Query returns NULL at the moment.

$args['meta_query'] = array(
    'relation' => 'OR',
    array( //THIS HAS 9 RESULTS
        'relation' => 'AND',
        array(
            'key' => 'dummy',
            'value' => [false, null],
            'compare' => '=',
        ),
        array(
            'key' => 'merk',
            'value' => $machine['machine_merk'][0],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'merk_type',
            'value' => $machine['machine_type'][0],
            'compare' => '=',
        ),
    ),
    array( //OR THIS HAS 30 RESULTS
        'relation' => 'AND',
        array(
            'key' => 'dummy',
            'value' => true,
            'compare' => '=',
        ),
        //and something else
    )
);

If I comment one of these 2 arrays to check, they both have results.

I have the following code which I think should work but obviously i'm missing something. Anyone who can tell why this is not correct?

//EDIT Here is what needs to happen. 'Merk', 'Type' and 'Dummy' are custom fields which are attributes for the product (not woocommerce).

I want to find all products that match either the 'Dummy' attribute false AND 'Merk' and 'Type' match the user input.

-- OR where 'Dummy' is true (AND another field 'Weight' will be matched, but firstly this should work also).

The new WP_Query returns NULL at the moment.

$args['meta_query'] = array(
    'relation' => 'OR',
    array( //THIS HAS 9 RESULTS
        'relation' => 'AND',
        array(
            'key' => 'dummy',
            'value' => [false, null],
            'compare' => '=',
        ),
        array(
            'key' => 'merk',
            'value' => $machine['machine_merk'][0],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'merk_type',
            'value' => $machine['machine_type'][0],
            'compare' => '=',
        ),
    ),
    array( //OR THIS HAS 30 RESULTS
        'relation' => 'AND',
        array(
            'key' => 'dummy',
            'value' => true,
            'compare' => '=',
        ),
        //and something else
    )
);

If I comment one of these 2 arrays to check, they both have results.

Share Improve this question edited Nov 22, 2023 at 8:53 Mart van der Kruis asked Nov 15, 2023 at 14:14 Mart van der KruisMart van der Kruis 11 bronze badge
Add a comment  | 

3 Answers 3

Reset to default 0

Just a couple of minor formatting changes: an extra nested array that can be deleted, and the incorrect 'value' when using the '=' compare operator, and an unnecessary 'relation' parameter.

$args['meta_query'] = array(
    // array( <- an extra array you don't need
        'relation' => 'OR',
        array(
            'relation' => 'AND',
            array(
                'key' => 'dummy',
                'value' => [false, null], // <- When using the '=' compare operator, the value cannot be an array, use an 'IN' compare value instead to see if the value of 'dummy' is 'IN' this array
                'compare' => '=',
            ),
            array(
                'key' => 'merk',
                'value' => $machine['machine_merk'][0],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'merk_type',
                'value' => $machine['machine_type'][0],
                'compare' => '=',
            ),
        ),
        // array( <-------------------|
            // 'relation' => 'AND', <-| Unnecessary as there is only one array being compared here
            array(
                'key' => 'dummy',
                'value' => true,
                'compare' => '=',
            ),
        //)
    //)
);

So it should look like this:

$args['meta_query'] = array(
    'relation' => 'OR',
    array(
        'relation' => 'AND',
        array(
            'key' => 'dummy',
            'value' => [false, null],
            'compare' => 'IN',
        ),
        array(
            'key' => 'merk',
            'value' => $machine['machine_merk'][0],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'merk_type',
            'value' => $machine['machine_type'][0],
            'compare' => '=',
        ),
    ),
    array(
        'key' => 'dummy',
        'value' => true,
        'compare' => '=',
    ),
);

Source: WP_Meta_Query

So when trying around I haven't gotten any further but maybe this is an even better example. This keeps returning NULL. On top of it all I need to clear all server cache every time because it stores the query even when i'm logged in and shouldn't do that.

$args['post_type'] = 'product';
$args['posts_per_page'] = 50;

//FROM CATEGORY
$args['tax_query'][] = array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => $params['category'],
    'operator' => 'IN'
);

//BUILD ARGS
$args['meta_query'] = array(
    'relation' => 'AND',
    array(
        'relation' => 'OR',
        array(
            array(
                'key' => 'merk',
                'value' => 'CAT',
                'compare' => '=',
            ),
            //if I comment following array it works
            array(
                'key' => 'merk_type',
                'value' => '300.9D',
                'compare' => '=',
            )
        ),
        array(
            //or if I comment following array it also works
            array(
                'key' => 'merk',
                'value' => 'KUBOTA',
                'compare' => '=',
            ),
            //and something else
        )
    ),
    //maybe something else
);

I am experiencing the same issue. Whenever I try to combine AND with nested OR queries, the query returns only posts that match both nested arguments, thus applying AND to the nested arguments instead of OR.

Still trying to figure this out, I'll report back if I find a solution (fingers crossed).

The meta_query array used (it's a dynamic generated query, so I can only provide the generated output):

Array
(
    [relation] => AND
    [0] => Array
        (
            [relation] => OR
            [0] => Array
                (
                    [0] => Array
                        (
                            [key] => motor_versions_$_filters
                            [compare] => LIKE
                            [value] => 9
                        )

                    [1] => Array
                        (
                            [key] => motor_versions_$_filters
                            [compare] => LIKE
                            [value] => 12
                        )

                )

        )

    [1] => Array
        (
            [relation] => OR
            [0] => Array
                (
                    [0] => Array
                        (
                            [key] => motor_versions_$_filters
                            [compare] => LIKE
                            [value] => 18
                        )

                    [1] => Array
                        (
                            [key] => motor_versions_$_filters
                            [compare] => LIKE
                            [value] => 16
                        )

                )

        )

)

本文标签: wp queryWhy is my nested 39OR39 metaquery not working