Home › Forums › General Issues › Setting up front end filtering and sorting by custom fields › Reply To: Setting up front end filtering and sorting by custom fields
Ok so I went through that tutorial – bedrooms was already one of the fields I need to sort by, so I thought it would be easy – however, I am having a lot of trouble with this still.
This is the code I put in my functions file:
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts( $query ) {
if( is_admin() ) { return; }
$meta_query = $query->get('meta_query'); // get original meta query
// validate type
if( empty($_GET['bedrooms']) )
{
return;
}
// get types as an array
// - use explode to get an array of values from type=a|b|c
$bedrooms = explode('|', $_GET['bedrooms']);
// set compare for the meta_query
// - http://codex.wordpress.org/Class_Reference/WP_Query
$meta_query['relation'] = 'OR';
foreach( $bedrooms as $bedroom )
{
$meta_query[] = array(
'key' => 'bedrooms',
'value' => '"' . $bedroom . '"',
'compare' => 'LIKE',
);
}
$query->set('meta_query', $meta_query); // update the meta query args
return; // always return
}
This is the code I have on the page I want to have filtering:
<div id="search-houses">
<?php
$field = get_field_object('bedrooms');
$values = explode(',', $_GET['bedrooms']);
?>
<ul>
<?php foreach( $field['choices'] as $choice_value => $choice_label ): ?>
<li>
<input type="checkbox" value="<?php echo $choice_value; ?>" <?php if( in_array($choice_value, $values) ): ?>checked="checked"<?php endif; ?> /> <?php echo $choice_label; ?></li>
</li>
<?php endforeach; ?>
</ul>
</div>
<script type="text/javascript">
(function($) {
$('#search-houses').on('change', 'input[type="checkbox"]', function(){
// vars
var $ul = $(this).closest('ul'),
vals = [];
$ul.find('input:checked').each(function(){
vals.push( $(this).val() );
});
vals = vals.join(",");
window.location.replace('<?php echo home_url('find-my-apartment'); ?>?bedrooms=' + vals);
console.log( vals );
});
})(jQuery);
</script>
This is the page: http://tlc.mainteractivegroup.com/find-my-apartment/
The first problem is that it is giving me a PHP error on this line:<?php foreach( $field['choices'] as $choice_value =>
The second problem is, when I add ?bedrooms=3 or any other number to the URL it goes to a 404 page.
The third thing I noticed – don’t know if this matters or not but when I add ?post_type=apartment to the URL it brings up the page just fine.
So I really need some help getting this part at least functional. I also don’t really understand completely how this function interacts with the existing loop I have on the page. You can see below the PHP error I have a loop that displays out all the apartments with the custom fields. Do I need to do anything to change that loop for this to work?
Thanks in advance!
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.