Support

Account

Home Forums Front-end Issues Advanced filtering of Queried ACF fields

Solving

Advanced filtering of Queried ACF fields

  • Active environment where the following will reside: http://test-hdwg.pantheon.io/projects/

    Hi all,
    Have a PHP based question, but would like to filter the results of a WP_Query based on the following (lamen written) algorithm utilizing ACF fields:

    If the query produces multiple results... {
      sort the results by ACF field “project_year_end” {
        if the field contains “present”, filter these items first {
          if multiple results, then {
            further sort by the field “project_year_start” value {}
          }
        }
        else if, sort those without “present” afterwards by the “project_year_end” value { 
          if multiple results for a single value (ex: 2015), then {
            further sort by “project_year_start” { }
          }
        }
      }
    }

    Currently, the template PHP that needs to include this query is built in the following fashion:

    <?php /* Template Name: Projects */ ?>
    <?php get_header(); ?>
    
            <main id="primary" class="content-area">
              <div class="jumbotron">
                <div class="row">
                  <div class="intro"><?php the_field('projects_heading') ?></div>
                </div>
              </div>
              <div class="projects">
                <div class="row project-list">
                  <div class="column-wrapper">
    <?php
      $args = array (
        'post_type'     => 'project', // target the post-type for projects
        'post_per_page' => '20', // display (at max) (x) at once
        'nopaging'      => true, // disable automatic pagination
        'order'         => 'ASC', // display order = ascending
        'orderby'       => 'title' // organized based on the the_title() of the post
      );
      $projects = new WP_Query( $args );
      if ( $projects->have_posts() ) { 
    ?>
                    <div class="project-listing">
    <?php while ( $projects->have_posts() ) {
      $projects->the_post();
    ?>
                      <div class="columns ui-listing-block">
                        <hr/>
                        <h2><?php the_field('project_title') ?></h2>
    <?php if( have_rows('project_date_range') ): ?>
                        <span>
    <?php while( have_rows('project_date_range') ): the_row(); ?>
    <?php the_sub_field('project_year_start'); ?>-<?php the_sub_field('project_year_end'); ?>
    <?php endwhile; ?>
                        </span>
    <?php endif; ?>
                      <?php echo project_excerpt(); ?>
                        <a href="<?php the_permalink(); ?>" title="Learn more about the project: <?php the_field('project_title') ?>" class="button postfix">Learn More</a>
                      </div>
    <?php } ?>
                    </div>
    <?php } else { ?>
                    <p>No Projects Available.</p>
    <?php } wp_reset_postdata(); ?>
                  </div>
                </div>
              </div>
            </main>
    <?php get_footer(); ?>

    Any help would be greatly appreciated!

  • Have you looked at the meta_query argument? You can pass this through your post arguments when you start the loop.

    https://codex.wordpress.org/Class_Reference/WP_Meta_Query

  • Hi @jodriscoll

    This can be achieved using PHP to order the array of posts.

    You will want to use the get_posts function instead of WP_Query query to obtain an array of posts.

    Looping through the array, you can load the custom field data and create an order array which you can then use to sort the $posts.

    This idea is covered in how to sort a repeater field: http://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/

    The only potential issue is that you have used a repeater field to allow multiple start / end dates. Perhaps you have done this for aesthetic reasons, but it will make the sorting much harder.

    Good luck

    Thanks
    E

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

The topic ‘Advanced filtering of Queried ACF fields’ is closed to new replies.