How to use WP_Query on a repeatable group
-
I am using a CMB2 repeatable group called ‘metric’ which is inside my custom ‘product_cpt’. Inside the ‘metric’ group are fields ‘type’ and ‘units’.
Example data would be like:
metric group 1
-Type = “A”
-Units = “1”
metric group 2
-Type = “B”
-Units = “2”I’m trying to build a query that searches for Type=”A” AND Units=”1″ across all metric groups, and return the products that have a match.
So far I have this but I’m not getting any results:
$meta_query = array('relation' => 'AND'); $meta_query[] = array( 'key' => '_product_cpt_metric_type', 'value' => 'A' ); $meta_query[] = array( 'key' => '_product_cpt_metric_units', 'value' => "1" ); $args = array( 'post_type' => 'product_cpt' 'orderby' => 'title', 'order' => 'ASC', 'nopaging' => true, 'meta_query' => $meta_query );
I think the problem is that the meta keys are wrong. In the db, the meta key is just ‘_product_cpt_metric’ and the value contains info for all groups. But then how do I specifically look for a combo of fields in one of the groups?
Looking at the database, ‘_product_cpt_metric’ groups for type=A,units=1 and type=B,units=2 look like this:
a:2:{i:0;a:2:{s:4:”type”;s:1:”A”;s:5:”units”;s:1:”1″;}i:1;a:2:{s:4:”type”;s:1:”B”;s:5:”units”;s:1:”2″;}}
- The topic ‘How to use WP_Query on a repeatable group’ is closed to new replies.