Support

Account

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

Solved

Querying by Taxonomy field and separating by one such taxonomy

  • I’m trying display specific custom post types by taxonomy on specific pages, and I’m attempting to follow in the footsteps of the advice found here: https://support.advancedcustomfields.com/forums/topic/query-posts-via-taxonomy-field/

    I’ve associated the custom post type taxonomies with the pages via the Taxonomy ACF field, and I’ve got that working fine.

    However I’m wondering if it’s possible make it so that I can divide the out put up by those taxonomies, similar to the solution found here:https://wordpress.stackexchange.com/a/40117

    But I can’t seem to successfully figure out how to marry the two concepts, so that the end result is that I’m only displaying the specific custom posts that relate to the taxonomies that I’ve selected on the target page via the Taxonomy field, separated by one of those taxonomies.

    Below is my unsuccessful attempt:

    
    <?php
    $post_id = $post->ID;
    $domains = get_field('matrix_domain', $post_id);
    $grades = get_field('matrix_grades', $post_id);
    if (!is_array($domains)) {
        $domains = array($domains);
      }
    if (!is_array($grades)) {
        $grades = array($grades);
      }
    
    $terms = get_terms( array(
      "taxonomy" => "math_grades",
    ) );
    foreach ($terms as $term) : 
      $posts = get_posts( array(
                    'post_type'         => 'math_matrix',
                    'posts_per_page'    => -1, //important for a PHP memory limit warning
                    'tax_query' => array(
    'relation' => 'AND',
          array(
            'taxonomy' => 'math_domains',
            'terms' => $domains,
          ),
          array(
            'taxonomy' => 'math_grades',
            'terms' => $grades,
          ),
                    ),
                ));
    ?>
      <h4><?= $term->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 foreach($posts as $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 endforeach; ?>
      </ul>
    <?php endforeach; ?>
    
    <?php
    $post_id = $post->ID;
    $domains = get_field('matrix_domain', $post_id);
    $grades = get_field('matrix_grades', $post_id);
    if (!is_array($domains)) {
        $domains = array($domains);
      }
    if (!is_array($grades)) {
        $grades = array($grades);
      }
    $_terms = get_the_terms( $post_id , 'math_grades' );
    
    foreach ($_terms as $term) :
    global $post; 
        $_posts = new WP_Query( array(
                    'post_type'         => 'math_matrix',
                    'posts_per_page'    => -1, //important for a PHP memory limit warning
                    'tax_query' => array(
    'relation' => 'AND',
          array(
            'taxonomy' => 'math_domains',
            'terms' => $domains,
          ),
          array(
            'taxonomy' => 'math_grades',
            'terms' => $grades,
          ),
                    ),
                ));
    
        if( $_posts->have_posts() ) : ?>
    
     <h3 class="grade"><?php echo $term->name; ?></h3>
            <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;
    
            echo '</ul>';
    
        endif;
        wp_reset_postdata();
    
    endforeach;
    ?>
    
  • 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; ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.