Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Alessio Baccarini
Alessio Baccarini
20,325 Points

Sort posts by ACF custom field AND display sorting key

I wonder if anyone can help me with this.

I have a wine list, perfectly sorted by subcat->ID (via a foreach loop) and by meta_key named 'regione' (that is, geographical area, or region).

What I'd like to do is to have the sorting meta_value display as a label/heading only ONCE per value before relevant loop results, like this:

REGION #1 -- wine 1 -- wine ... -- wine 12

REGION #2 -- wine 1 -- wine ... -- wine 8.

...and so on (obviously, the above numbers are totally arbitrary).

Yet, I can't seem to get it right... .

Here's my code:

<ul class="pietanze_vini"> <?php $cat = 'Vini bianchi'; $catID = get_cat_ID($cat); $subcats = get_categories('child_of=' . $catID); foreach ( array_reverse($subcats) as $subcat ) : ?> <li class="subcat"> <?php echo '<h3>' . $cat . ' ' . $subcat->cat_name . '</h3>'; ?> <ul> <?php $my_query = new WP_Query(array( 'post_type' => 'carta_dei_vini', 'posts_per_page' => -1, // max number of post per category 'category__in' => array($subcat->term_id), 'meta_key' => 'regione', 'orderby' => 'meta_value', 'order' => 'ASC'

         ));
         if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <li>
            <dl>
                <dt><?php the_title(); ?></dt>
                <?php
                    $prezzo = get_field('prezzo');
                    if( !empty($prezzo)) {
                        echo '<dd class="prezzo_vino">' .$prezzo . '</dd>';
                        }
                    $annata = get_field('annata');
                    if( !empty($annata)) {
                        echo '<dd class="annata">'.$annata.' '. '|' . ' '.'</dd>';
                        }
                    $gradazione = get_field('gradazione_alcolica');
                    if( !empty($gradazione)) {
                        echo '<dd class="gradazione">' . $gradazione . '&deg;'. '</dd>';
                        }
                    $regione = get_field('regione');
                    if( !empty($regione)) {
                        echo '<dd class="regione">' . $regione . '</dd>';
                        }
                    $produttori = get_field('produttore');
                        foreach ( $produttori as $produttore ) :
                            echo '<dd class="produttore">' . 'Da'. ' '. $produttore->post_title . '</dd>';
                        endforeach;
                    $quantita = get_field('quantita');
                    if( !empty($quantita) && $quantita !="750 Ml") {
                        echo '<dd class="quantita">, ' .$quantita . '</dd>';
                        }
                    $denominazione = get_field('denominazione');
                    if( !empty($denominazione)) {
                            echo '<dd class="denomazione">' . 'Denominazione:' . ' ' .$denominazione . '</dd>';
                        }
                    ?>
            </dl>
        </li>
        <?php endwhile; endif; ?>
    </ul>
</li>

<?php endforeach; ?> <?php wp_reset_postdata(); ?> </ul>

Needless to say, the geographical area field gets flawlessly printed within the loop (as in $regione = get_field('regione'); etcetera).

Any help would be greatly appreciated!

Thanks in advance,

Alessio

1 Answer

Alessio Baccarini
Alessio Baccarini
20,325 Points

A friend on the ACF forum provided the solution to the above question, hence I'd like to share it here:

The posts are sorted by field 'regione', hence

<?php
// the last region displayed, set it to start as empty
$last_regione = ''; 

while ($my_query->have_posts()) {
    $my_query->the_post();

    $this_regione = get_field('regione');

    // test to see if we've changed regions
    // this will always be true for the first post
    if ($this_regione != $last_regione) {

        // code here to echo new region heading

    }

    // set last region to this region for the next loop, so that no 'regione' value will be echoed until it changes to a new value
    $last_regione = $this_regione;

    // remainder of your code to display post continues here

} // end while have posts.

?>

This worked perfectly!

Thank you, John Huebner (http://support.advancedcustomfields.com/forums/users/hube2/)