WP_Query meta_query -- more than 2-3 items?

389 views
Skip to first unread message

relish27

unread,
Jun 26, 2014, 1:38:28 PM6/26/14
to mpls-stpau...@googlegroups.com
Another wp_query question for yous... has anyone ever encountered a need for more than two items in a wp_query meta_query array?  So you know that this is how you do it with two:

$args = array(
	'post_type' => 'product',
	'meta_query' => array(
		'relation' => 'OR',
		array(
			'key' => 'color',
			'value' => 'blue',
			'compare' => 'NOT LIKE'
		),
		array(
			'key' => 'price',
			'value' => array( 20, 100 ),
			'type' => 'numeric',
			'compare' => 'BETWEEN'
		)
	)
);
$query = new WP_Query( $args );
I have a scenario where I'd like to have a dynamic number of meta_queries in there... and, often, more than just two.  I'll output my args array below so you can see the kind of info I'm wanting to search on.   (And for why I'm using compare like that, you can see the explanation about querying ACF relationship fields here, down by the "single-location.php" section).

Unless I'm not seeing it, I don't see the wp_query codex saying anything about a limit here... but I found that if I had more than three of these sales_rep_region arrays, the query failed.  Three or less worked.  Weird?  It may be that I just need to build my own custom SQL query, which I can do, but I prefer to use wp_query if I can.


-----------

Array
(
    [post_type] => sales-rep
    [post_status] => publish
    [posts_per_page] => -1
    [orderby] => menu_order
    [order] => ASC
    [meta_query] => Array
        (
            [relation] => OR
            [0] => Array
                (
                    [key] => sales_rep_regions
                    [value] => "236"
                    [compare] => LIKE
                )

            [1] => Array
                (
                    [key] => sales_rep_regions
                    [value] => "245"
                    [compare] => LIKE
                )

            [2] => Array
                (
                    [key] => sales_rep_regions
                    [value] => "254"
                    [compare] => LIKE
                )

            [3] => Array
                (
                    [key] => sales_rep_regions
                    [value] => "265"
                    [compare] => LIKE
                )

            [4] => Array
                (
                    [key] => sales_rep_regions
                    [value] => "269"
                    [compare] => LIKE
                )

        )

)


Nicholas Ciske

unread,
Jun 26, 2014, 2:44:42 PM6/26/14
to mpls-stpau...@googlegroups.com
I've done more than 3 but not on the same field.

My guess is you may be hitting a recursion or nested query limit in MySQL?

Breaking this into 2 queries - a custom query to get post IDs that match, then a WP_Query using IN - would be more efficient anyways. I think. ;-)

_______________________
Nick Ciske
@nciske
http://ThoughtRefinery.com

Sent from my iPhone 5

Courtney Remes

unread,
Jun 26, 2014, 4:45:23 PM6/26/14
to mpls-stpau...@googlegroups.com
Good idea!  Thanks!


- Courtney


--
You received this message because you are subscribed to the Google Groups "Minneapolis St. Paul WordPress User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mpls-stpaul-word...@googlegroups.com.
To post to this group, send email to mpls-stpau...@googlegroups.com.
Visit this group at http://groups.google.com/group/mpls-stpaul-wordpress.
For more options, visit https://groups.google.com/d/optout.

Courtney Remes

unread,
Jun 26, 2014, 5:25:43 PM6/26/14
to mpls-stpau...@googlegroups.com
I was excited for a minute there... but I don't think that'll work.  Ultimately, I'm still searching on a CPT's ACF relationship field ("sales_rep_regions" in the args output there).  And because relationship fields are serialized, you have to use that weird compare LIKE query, which only works one ID at a time.  No IN query available.  I'll chew on this some more, but it may be that one custom query is what needs to happen.


- Courtney

Nick Ciske // Thought Refinery

unread,
Jun 26, 2014, 5:34:21 PM6/26/14
to mpls-stpau...@googlegroups.com
Query 1 (straight MySQL LIKE):

$regions =  SELECT post_id FROM wp_post_meta WHERE meta_key = "sales_rep_regions" AND ( meta_value LIKE X OR  meta_value LIKE Y ... )

Query 2 (WP_Query): 

'posts_in' => $regions,

No massive joins required -- just straight queries on indexed columns.

_________________________
Nick Ciske
@nciske
Did I help you? Say thanks: http://bit.ly/1lahwy0
Reply all
Reply to author
Forward
0 new messages