I am using ACF (Advanced Custom Fields) and filtering the results using the method they posted on their website.


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

In category.php

    <div id="search-houses">

    $field = get_field_object('bedrooms');
    $values = explode(',', $_GET['bedrooms']);

        <?php foreach( $field['choices'] as $choice_value => $choice_label ): ?>
                <input type="checkbox" value="<?php echo $choice_value; ?>" <?php if( in_array($choice_value, $values) ): ?>checked="checked"<?php endif; ?> /> <?php echo $choice_label; ?></li>
        <?php endforeach; ?>
<script type="text/javascript">
(function($) {

    $('#search-houses').on('change', 'input[type="checkbox"]', function(){

        // vars
        var $ul = $(this).closest('ul'),
            vals = [];


            vals.push( $(this).val() );


        vals = vals.join(",");

        window.location.replace(location.pathname + '?bedrooms=' + vals);

        console.log( vals );



Demo: http://thepursesociety.com/_web/category/houses

| improve this question | | | | |
  • Have you read the Codex page for pre_get_posts? – Milo Mar 11 '14 at 4:22
  • I did and tried to understand, but my knowledge is very limited with PHP and coding. Do you know what am I doing wrong, and how can I fix it? – user3137901 Mar 11 '14 at 14:54

From pre_get_posts in Codex:

Identifying Target Queries

When using pre_get_posts, be aware of the query you are changing. One useful function is is_main_query(), which can help you ensure that the query you are modifying is only the main query. Use it in combination with other conditional tags to only alter the main query on the pages you want.

pre_get_posts is executed for every query on a page, which includes the query that loads your menu items, so you need to check if $query->is_main_query() before you modify it, otherwise you'll apply your query modifications to other queries.

| improve this answer | | | | |
  • Thanks a million! Solved it. Theme was using $query->is_main_query(). – user3137901 Mar 12 '14 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.