0

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

http://www.advancedcustomfields.com/resources/tutorials/creating-wp-archive-custom-field-filter/

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>

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

  • 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
0

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.

  • 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.