• Resolved bjacobs09

    (@bjacobs09)


    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?

    This is what the meta_value in the database for the meta_key ‘_product_cpt_metric’ that has a group for type=A,units=1 and type=B,units=2:
    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″;}}

Viewing 1 replies (of 1 total)
  • Thread Starter bjacobs09

    (@bjacobs09)

    I figured it out:

    $value = array('type'=>'A', 'units'=> '1');
                      $meta_query[] = array(
                        'key' => $meta_key,
                        'value' => serialize( $value ),
                        'compare' => 'LIKE'
                        );
Viewing 1 replies (of 1 total)

The topic ‘Using WP_Query for a repeatable group’ is closed to new replies.