Support

Account

Home Forums ACF PRO Querying by Taxonomy field and separating by one such taxonomy Reply To: Querying by Taxonomy field and separating by one such taxonomy

  • Figured it out. I needed to write a foreach for the specific taxonomy I wanted to separate the posts by, but also place the individual version of that foreach statement into the tax_query.

    
    <?php
    
    $domains  = 'math_domains';
    $grades = 'math_grades';
    
    $domainterms = get_field( 'matrix_domain' );
    $gradeterms = get_field( 'matrix_grades' );
    
      if (!is_array($domainterms)) {
        $domainterms = array($domainterms);
      }
      if (!is_array($gradeterms)) {
        $gradeterms = array($gradeterms);
      }
    
    $pagegrades = get_terms( 'math_grades' );
    
    foreach( $pagegrades as $pagegrade ) : ?>
    
    <?php
        $args = array(
            'post_type'      => 'math_matrix',
            'posts_per_page' => -1,
        'tax_query' => array(
          'relation' => 'AND',
          array(
            'taxonomy' => $domains,
            'field' => 'term_id',
            'terms' => $domainterms,
          ),
          array(
            'taxonomy' => $grades,
            'field' => 'term_id',
            'terms' => $pagegrade,
          ),
        ),
        );
    
        $posts = new WP_Query( $args ); ?>
    <?php if( $posts->have_posts() ): ?> 
    <h4><?php echo $pagegrade->name ;?></h4>
    <div class="matrix-labels"><div class="standard-label">Standard</div><div class="can-label">Can the student:</div><div class="cannot-label">If not:</div><div class="resource-label">Additional Resource</div></div>
    <ul class="matrix-search">
                        
    
                <?php while( $posts->have_posts() ) : $posts->the_post(); ?>
    <li><div class="matrix-standard"><div class="mobile-label">Standard</div><?php global $post;
    $standards = wp_get_post_terms($post->ID, 'math_standards', array("fields" => "names"));
    if (count($standards) > 0)
    {
        echo implode(', ', $standards);
    }; ?></div>
           <div class="matrix-title"><div class="mobile-label">Title</div><?php the_title(); ?></div>
           <div class="matrix-action"><div class="mobile-label">Action</div><a>" target="_blank"><?php echo esc_html($actionsTitle); ?></a></div>
           <div class="matrix-resource"><div class="mobile-label">Additional Resource</div><a>" target="_blank"><?php echo esc_html($resourcesTitle); ?></a></div>
    </li>
                <?php endwhile; endif; ?>
    
    </ul>
    <?php endforeach; ?>