Hi, I’ve been having difficulty successfully implementing a filter on a custom post type archive page using Advanced Custom Fields. The query string loads as it should, and returns the correct results. However, it also returns a foreach error on the field choices, and the filter itself disappears. I’ve searched through the ACF and Themeco forums and had no success resolving the issue, and I’ve had a fellow developer look at it with no resolution. Here’s the code from functions.php:
$GLOBALS['my_query_filters'] = array(
'Room Type' => 'room_type',
'Medium' => 'grow_medium',
);
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts( $query )
{
if ( !is_admin() && is_post_type_archive('growdiaries') && (isset($_GET['room_type']) || isset($_GET['grow_medium'])) ) {
$meta_query = $query->get('meta_query');
foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
if( empty($_GET[ $name ]) ) {
continue;
}
$value = explode(',', $_GET[ $name ]);
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
);
}
$query->set('meta_query', $meta_query);
}
return;
}
And here's the relevant section from my archive-CPT.php:
<div id="archive-filters" style="margin-bottom: 1em;padding-bottom:1em;border-bottom: 1px solid #e1e1e1;">
<div><p class="details-p" style="text-align:left;margin-left:0 !important;border-bottom: 1px solid #e1e1e1;width:auto;">Filter By: </p></div>
<?php foreach( $GLOBALS['my_query_filters'] as $key => $name ):
// get the field's settings without attempting to load a value
$field = get_field_object($key, false, false);
$choice_value = '';
// set value if available
if( isset($_GET[ $name ]) ) {
$field['value'] = explode(',', $_GET[ $name ]);
}
// create filter
?>
<div class="filter" data-filter="<?php echo $name; ?>">
<?php
$values = get_field($name);
$field = get_field_object($name, false, false);
$choices = $field['choices'];
?>
<p class="details-p" style="text-align:left;margin-left:0 !important;"><?php echo $key; ?>
<?php $values = isset($_GET[ $name ]) ? explode(',', $_GET[ $name ]) : array();
foreach( $field['choices'] as $choice_value => $choice_label ): //invalid foreach argument here ?>
<span style="text-transform: capitalize; margin: 0 0.2em;">| <?php echo $choice_label; ?></span>
<input type="checkbox" value="<?php echo $choice_value ?>" <?php if( in_array($choice_value, $values) ): ?> checked="checked" <?php endif; ?> />
<?php endforeach; ?>
</p>
</div>
<?php endforeach; ?>
</div>
<script type="text/javascript">
(function($) {
// change
$('#archive-filters').on('change', 'input[type="checkbox"]', function(){
// vars
var url = '<?php echo home_url('growdiaries'); ?>';
args = {};
// loop over filters
$('#archive-filters .filter').each(function(){
// vars
var filter = $(this).data('filter'),
vals = [];
// find checked inputs
$(this).find('input:checked').each(function(){
vals.push( $(this).val() );
});
// append to args
args[ filter ] = vals.join(',');
});
// update url
url += '?';
// loop over args
$.each(args, function( name, value ){
url += name + '=' + value + '&';
});
// remove last &
url = url.slice(0, -1);
// reload page
window.location.replace( url );
});
})(jQuery);
</script>
I’m not sure why my code isn’t formatting correctly, sorry about that… Any insight much appreciated!