1

I'm wanting to expand on the following query to look at multiple meta_key and meta_value values that have been created with the Advanced Custom Fields (ACF) plugin.

e.g.

meta_key 'colours_%_colour' meta_value 'Red'

AND

meta_key 'colours_%_design_style' meta_value 'Plain'

This is what I have so far which I came across on the ACF Documentation (https://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/)

It successfully retrieves results with the color (sub field) of red.

$rows = $wpdb->get_results($wpdb->prepare(

"
SELECT *
FROM {$wpdb->prefix}postmeta
WHERE meta_key LIKE %s
    AND meta_value REGEXP %s
",
'colours_%_colour', // meta_name
'red' // meta_value

));

Thanks,

Matt.

0

is this what you're after? Using the "Relation" for the Meta Query, you can select multiple Meta Keys. You can change the "Compare" to any of the operations available for that function. (From the Codex) compare (string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' and 'NOT EXISTS'. Default value is '='.

// WP_Query arguments
    $args = array (
            'post_type'          => 'your-post-type',
                'meta_query'          => array(
                'relation' => 'AND', 
             array(
                 'key'       => 'colours_%_colour',
                 'value'     => 'Red',
                 'compare'   => 'LIKE',
                    ),

             array(
                 'key' => 'colours_%_design_style',
                 'value' => 'Plain',
                 'compare' => 'Like'
                    ),
                 ), 
             );

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // do something
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.