Home › Forums › Front-end Issues › Top menu disapears in Category Pages when filtering using ACF
Example:
http://thepursesociety.com/_web/category/houses
Filtering works fine, however, top menu disappears when the result is displayed. The code I am using is below:
In functions.php
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts( $query ) { // validate if( is_admin() ) { return; }
// get original meta query
$meta_query = $query->get('meta_query');
// allow the url to alter the query
// eg: http://www.website.com/events?location=melbourne
// eg: http://www.website.com/events?location=sydney
if( !empty($_GET['bedrooms']) )
{
$bedrooms = explode(',', $_GET['bedrooms']);
//Add our meta query to the original meta queries
$meta_query[] = array(
'key' => 'bedrooms',
'value' => $bedrooms,
'compare' => 'IN',
);
}
// update the meta query args
$query->set('meta_query', $meta_query);
// always return
return;
}
In category.php
<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(location.pathname + '?bedrooms=' + vals);
console.log( vals );
});
})(jQuery);
</script>
Hi @uakz
Your pre_get_posts
filter has the potential to affect every WP_Query on the page! This would explain why your top menu is not returning the correct data.
Looking at the WP docs: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts it seems that another validation is done in the filter to make sure this is the main
query on the page:
if ( !is_admin() && $query->is_main_query() ) {
So you could add another line to the top of your filter like this:
// validate if( !$query->is_main_query() ) { return; }
hope that helps
The topic ‘Top menu disapears in Category Pages when filtering using ACF’ 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.