Home Forums Front-end Issues 2 Problems with custom archive


2 Problems with custom archive

  • I’m trying to make a custom archive with filters (like this tutorial) on two fields. Although both fields are type=Select, just like the tutorial, one of them allows multiple values, and that seems to be causing problems. A URL like (day_of_week is the field that can have multiple values for a given event) should find records – at the moment there are two events with Sunday selected. But apparently they are not found. The other field works fine with identical code – you can try and it will work.

    The second problem is that when the filter finds no matching records, get_field_object() doesn’t return the array of field information needed to to build the sets of checkboxes. I don’t know why that happens, but it happens in the tutorial, too – look at 11:05 on the video and you’ll see the checkboxes disappear (his doesn’t have an error like mine does, and I could of course prevent the error, but I want the checkboxes!).

    Here is my entire in-progress template (I’m using StartUpPro theme, if that matters):

    <?php get_header(); ?>
    <section id="content" role="main">
      <header class="header">
        <h1 class="entry-title"><?php _e( 'OIC Events', 'startup' ); ?></h1>
        <div id="filter-oicevents" class="custom-filter">
    function make_checkboxes($field,$values) {
        <span class="checklist-title"><?php printf(__("Filter by %s"),$field['label']); ?>: </span>
        <ul id="<?php echo $field['_name']; ?>" class="checkbox-list">
      <?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>
      <?php endforeach; ?>
    make_checkboxes(get_field_object('event_category'),explode(',', $_GET['event_category']));
    make_checkboxes(get_field_object('day_of_week'),explode(',', $_GET['day_of_week']));
    <script type="text/javascript">
    (function($) {
      $('#filter-oicevents').on('change', 'input[type="checkbox"]', function(){
        var url = "<?php echo home_url('events') ?>";
        $('#filter-oicevents').find('ul.checkbox-list').each(function() {
          url += (url == "<?php echo home_url('events') ?>" ) ? "?" : "&";
          url += $(this).attr("id") + "=";
          vals = [];
            vals.push( $(this).val() );
          vals = vals.join(",");
          //console.log( vals );
          url += vals;
    if ( have_posts() ) :
      while ( have_posts() ) :
      <article id="oicevent-<?php the_ID(); ?>" <?php post_class(); ?>>
          <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    <?php get_template_part( 'entry', 'summary' ) ); ?>
    get_template_part( 'nav', 'below' );
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  • Ignore the first question – I gave up completely on allowing multiple selections, as it ran into too many problems and the case where we might need more than one is purely theoretical, so I decided to keep it simple for now.

    But the second issue needs solving if I’m going to have these filters at all. As I said, the problem can easily be seen on the tutorial video at timecode 11:05, so it’s not something unique to my code. If anyone else has implemented such a filter, how did you deal with get_field_object() failing when the result set is empty? This looks like a bug in get_field_object() to me.

  • Okay, I finally figured this out on my own. I studied the get_field_object() function more deeply, and on this documentation page, in the “field_key vs field_name” section, there is a hint. It is not said directly, but apparently even if posts have been created that use the Field Group, if a filter causes the current query of posts (using the pre_get_posts hook) to return none of them, field_name won’t work. Even Elliot used the field_name in his tutorial, but in the case of a filter like this, you MUST use the field_key, even though it makes the code harder to read.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.