Home › Forums › General Issues › Custom field filter using Radio Button
In short I have a CPT for School Directory (dwb_directory) and an archive page showing all post. I have CFT ‘position_type’ that has just two options:
teacher : Teacher
staff : Staff
I followed the tutorial “Creating a WP archive with custom field filter” and was able to filter the directory successfully by clicking Staff or Teacher. NOTE: I am aware that the tutorial was for checkboxes. Perhaps doing this is not appropriate for Radio buttons but i figured give it a shot.
The CPT archive url is http://dev.woodslearningcenter.org/school-directory/
Clicking either option gives me appropiately either:
http://dev.woodslearningcenter.org/school-directory/?position_type=staff
or
http://dev.woodslearningcenter.org/school-directory/?position_type=teacher
Problems:
1) The Staff Radio Button is selected by default at the main archive url.
2) When a choice is made the right posts are filterd but an error at the top rears its ugly head:
Error
Warning: Illegal offset type in isset or empty in W:\wamp\www\woodslearningcenter\wp-content\plugins\advanced-custom-fields-pro\fields\radio.php on line 115
My Archive page for the Directory:
add_action( 'genesis_before_loop', 'genesis_extender_archive_school_directory_filter_hook_box', 10 );
function genesis_extender_archive_school_directory_filter_hook_box() {
genesis_extender_archive_school_directory_filter_hook_box_content();
}
function genesis_extender_archive_school_directory_filter_hook_box_content() { ?>
<?php
if(is_post_type_archive('dwb_directory')): ?>
<div id="archive-filters">
<?php
wp_reset_postdata();
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);
// set value if available
if( isset($_GET[ $name ]) ) {
$field['value'] = explode(',', $_GET[ $name ]);
}
// create filter
?>
<div class="filter" data-filter="<?php echo $name; ?>">
<?php create_field( $field ); ?>
</div>
<?php endforeach; ?>
</div>
<script type="text/javascript">
(function($) {
// change
$('#archive-filters').on('change', 'input[type="radio"]', function(){
// vars
var url = '<?php echo home_url('school-directory'); ?>';
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>
My Functions.php
// array of filters (field key => field name)
$GLOBALS['my_query_filters'] = array(
'position_type' => 'position_type'
);
// action
add_action('pre_get_posts', 'filter_school_directory_pre_get_posts', 10, 1);
function filter_school_directory_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ) {
return;
}
// get meta query
if ($query->is_main_query()){
// get original meta query
$meta_query = $query -> get('meta_query');
// loop over filters
foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
// continue if not found in url
if( empty($_GET[ $name ]) ) {
continue;
}
// get the value for this filter
// eg: http://www.website.com/events?city=melbourne,sydney
$value = explode(',', $_GET[ $name ]);
// append meta query
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
);
}
// update meta query
$query->set('meta_query', $meta_query);
}
}
The problem is that in $GLOBALS['my_query_filters']
you need to pass it the key
and then the name
, but you are passing the name in twice. Because of this it isn’t able to call get_field_object()
correctly, thus can’t determine the field type
, thus the error.
The topic ‘Custom field filter using Radio Button’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.