Using get_posts, I need to use a combination of OR & AND relations

Using WordPress v4.6, I set up arguments for get_posts() based on price (2 conditions), sku, and categories like this:

[meta_query] = Array
    (
        [relation] = AND
        [0] = Array
            (
                [key] = _price
                [value] = Array
                    (
                        [0] = 0
                        [1] = 5
                    )

                [type] = DECIMAL
                [compare] = BETWEEN
            )

        [1] = Array
            (
                [key] = _price
                [value] = Array
                    (
                        [0] = 50.01
                        [1] = 100
                    )

                [type] = DECIMAL
                [compare] = BETWEEN
            )

        [2] = Array
            (
                [key] = _sku
                [value] = batte
                [compare] = like
            )

    )

[tax_query] = Array
    (
        [0] = Array
            (
                [taxonomy] = product_cat
                [field] = id
                [terms] = Array
                    (
                        [0] = 26
                        [1] = 20
                    )

            )

    )

I set AND relations, but actually I need OR relations between 2 prices and AND relations for the rest of the comparisons.

What is the correct syntax for this query?

Topic tax-query meta-query get-posts Wordpress

Category Web


Nested arrays can be used to create complex Meta Query's.

An example of how this would apply to your situation.

$meta_query_args = array(
    'relation' => 'AND', 
    array(
        // Conditions
    ),
    array(
        'relation' => 'OR',
        array(
            // Conditions
        ),
        array(
            // Conditions
        )
    )
);

For more information check: https://codex.wordpress.org/Class_Reference/WP_Meta_Query

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.