I’m wanting to expand on the following query to look at multiple meta_key and meta_value values that have been created within ACF.
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.
Hi @matthewhaigh
It depends on how you want the subfields to show up. Here’s an example if you want to show any subfields with certain value:
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM {$wpdb->prefix}postmeta
WHERE
( meta_key LIKE %s AND meta_value = %s) OR
( meta_key LIKE %s AND meta_value = %s)
",
'colours_%_colour', 'red',
'images_%_type', 'type_3'
));
You can combine it with any keys and values you want. To learn more about the AND
and OR
syntax, please take a look at this page: http://www.hackingwithphp.com/9/3/13/multiple-where-conditions.
Also, I’m afraid this is a MySQL topic. For further support, please visit WordPress support forum or MySQL community instead.
Thanks 🙂
Hi James
Thanks for taking time to answer my question, the support is appreciated.
Thanks,
Matt