I have a repeater field setup for a CPT, Field name is “Zip Codes” and the sub field is “Zip Code”.
I am trying to use those fields as part of an SQL query where I can search if any of the entered zip codes matches a search criteria.
Anyone know how to grab and iterate over repeater fields in the database?
Thanks.
You could iterate over the repeater field adding the values to an array, then use that array to make the sql statement.
Like:
<?php
$my_sql = array();
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
//add value to sql array
array_push($my_sql, get_sub_field('zip_code');
endwhile;
else :
// no rows found
endif;
$sql = "SELECT * FROM table_name WHERE field_name IN (".implode(",",$my_sql).")";
?>