I have been working through this example: http://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

That a colleague of mine found for creating a simple checkbox-based filter for Wordpress custom posts, that filters by a custom post type.

I've got it all set up and it seems to be working here - , if you filter by 'Place' then the 'All Locations' post is removed as you would expect.

However if you choose an option that isn't there, the filters disappear on the refresh and I cannot figure out why. My colleague has used this exact code on another site that doesn't have this problem so I'm left stumped. I've tried reordering the elements in the template in case something is affecting it but nothing seems to change.

The code in my archive-jobs.php :

  <div class="filterOptions">

    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean nisl odio, efficitur non lorem ut, volutpat egestas tellus. Vestibulum suscipit sem mi, nec mattis tortor convallis in. Integer in blandit turpis, consectetur tincidunt dolor.</p>

      <div id="search-location">

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

              <?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-location').on('change', 'input[type="checkbox"]', function(){

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


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


              vals = vals.join(",");

              window.location.replace('<?php echo home_url('jobs'); ?>?location_choice=' + vals);

              console.log( vals );



Code from functions.php:

        add_action('pre_get_posts', 'my_pre_get_posts');

    function my_pre_get_posts( $query )
        // validate
        if( is_admin() )

        if( !$query->is_main_query() )

        // 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['location_choice']) )
                $location_choice = explode(',', $_GET['location_choice']);

                //Add our meta query to the original meta queries
                $meta_query[] = array(
                    'key'       => 'location_choice',
                    'value'     => $location_choice,
                    'compare'   => 'IN',

        // update the meta query args
        $query->set('meta_query', $meta_query);

        // always return


Any help would be much appreciated!


When there are no posts, there is no post ID for get_field_object to operate with. If you read the docs for that function, you'll see:

$post_id Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc

  • Thank you for the response, that makes sense logically but leaves me wondering why the same wouldn't be true for my colleague who used the same code here for the brand filters - watchbuyers.co.uk/watches. When 'Jaeger Le Coultre' is filtered there are no results but the filters stay. Is there a workaround or suggested method of doing this that you could point me toward at all? – Kyuuji Jan 30 '15 at 16:19
  • 2
    have you tried using the field key rather than the name? – Milo Jan 30 '15 at 16:34
  • just did and it's working. I had no idea about the key and got it from the documentation. I can't believe that they would a) have it hidden by default and b) use the name in the tutorial when they categorically state the key should be sued 100% of the time. Thank you for the help, that link was invaluable. – Kyuuji Jan 30 '15 at 16:41

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.