• 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?

    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″;}}

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Yeah, the biggest issue you’re seeing is the fact that the data is being stored as serialized data. If they were basic CMB2 fields, I believe things would be fine, but due to how groups work and how they’re saved together, it end up with what you’re seeing above. The meta queries are trying to find exact values in individual meta rows, but that is not the case.

    It’s going to be an all in all hairy situation, and potentially return unintended results, but it *may* be possible to get some results with the “LIKE” value for meta_compare. https://codex.www.ads-software.com/Class_Reference/WP_Query#Custom_Field_Parameters Part of the hairy situation part is that it’s one key that you’d be trying to check for multiple values in. I’m not guaranteeing anything here, just pointing out potential solutions.

    Thread Starter bjacobs09

    (@bjacobs09)

    Thank you. Yes I just figured that out. The solution is:

    $value = array('type'=>'A', 'units'=> '1');
                      $meta_query[] = array(
                        'key' => $meta_key,
                        'value' => serialize( $value ),
                        'compare' => 'LIKE'
                        );
    • This reply was modified 7 years, 1 month ago by bjacobs09.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Glad you found something that will work for you. Not a fun situation all in all.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to use WP_Query on a repeatable group’ is closed to new replies.