WordPress WP_Query Limited to 5 Meta Query Clauses

570 Views Asked by At

The query works if you change "publish" to "draft" or remove the source clause. These are args passed to the WP_Query constructor. I immediately count the results to verify that WP_Query is in fact returning no results. I checked the data in the database and made sure the data is congruent.

Array
(
    [post_type] => post
    [post_status] => Array
        (
            [0] => publish
        )

    [meta_query] => Array
        (
            [view_count_clause] => Array
                (
                    [key] => _viewcount
                    [type] => NUMERIC
                )

            [actual_date_clause] => Array
                (
                    [key] => _actualdate
                )

            [start_date_clause] => Array
                (
                    [key] => _actualdate
                    [value] => 2021-03-23 00-00-00
                    [compare] => >
                )

            [end_date_clause] => Array
                (
                    [key] => _actualdate
                    [value] => 2021-03-24 00-00-00
                    [compare] => <
                )

            [color_clause] => Array
                (
                    [key] => _color
                    [type] => NUMERIC
                    [compare] => IN
                    [value] => Array
                        (
                            [0] => 4
                        )

                )

            [source_clause] => Array
                (
                    [key] => _sourcename
                    [value] => AHA
                    [compare] => =
                )

        )

    [orderby] => Array
        (
            [actual_date_clause] => DESC
        )

)
1

There are 1 best solutions below

0
Keith Gardner On

Related question here.... How to avoid the limit on the number of AND conditions for custom field (meta value) queries in WordPress get_posts()?

Problem:

WP_Query limits meta query clauses to five meta clauses. If you have 6 meta query clauses, you will get no results.

Solution 1:

Write your own SQL query. Example provided by the related question.

Solution 2:

Remove excess meta queries beyond the 5 meta query limit for WP_Query and manually filter results. Choice of meta queries to be removed should be based on whatever would result in the smallest data set.