Support

Account

Home Forums ACF PRO Sort posts by ACF custom field AND display sorting meta_value

Solved

Sort posts by ACF custom field AND display sorting meta_value

  • 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

  • I’m not quite sure how to work this into your code, I’m not sure where you want to output the unique value or how to work it into the list structure you have going.

    If the posts are sorted by regione then you can to something like this:

    
    // the last region displayed, starts 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 display new region heading
            
        }
    
        // set last region to this region for the next loop
        $last_regione = $this_regione;
    
        // remainder of your code to display post continues here
        
    } // end while have posts
    
  • John,

    this is PERFECT.

    Thanks a bunch,

    Alessio

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

The topic ‘Sort posts by ACF custom field AND display sorting meta_value’ is closed to new replies.