Support

Account

Home Forums Add-ons Repeater Field Orderby title instead of foreach Reply To: Orderby title instead of foreach

  • Ok, just checked out the code on a demo site I created with your XML file. I’m not sure, but we might forgot to tell WordPress about your post_type => ‘prodotti’ in the query. Could you please check if the following code works for you?

    <?php if ( get_field( 'schede_prodotti' ) ): ?>
    <table class="product-list simple-list">
        <?php while( has_sub_field( 'schede_prodotti' ) ): ?>
        <tr>
            <td class="title">
            <?php
                /**
                 * Get the IDs
                 *
                 * Returns an array of the IDs of the posts
                 * selected using the Relationship field.
                 *
                 * @see Screenshot: Change return value from Post Objects to Post IDs
                 * @var array
                 */
                $ids = get_sub_field( 'relazione_prodotti' );
                // var_dump( $ids ); //uncomment this line to see the $ids getting loadded
    
                /**
                 * Prepare the get_posts arguments
                 *
                 * With the 'orderby' => 'ttile' we accomplish the goal of having the
                 * products ordered by name instead of drag and drop.
                 *
                 * @see http://codex.wordpress.org/Template_Tags/get_posts#Parameters
                 * @var array
                 */
                $args = array(
                    'post__in'  => $ids,    // Use the ids from above
                    'order'     => 'ASC',   // Order
                    'orderby'   => 'title',  // Field to order the posts by
                    'post_type' => 'prodotti' // We might forgot about your post type...
                );
    
                /**
                 * Query the Posts
                 *
                 * @var array
                 */
                $posts = get_posts( $args );
    
                if ( $posts ) :
    
                    foreach ( $posts as $post ) : setup_postdata( $post ); ?>
    
                        <span><?php the_title(); ?></span><?php
    
                    endforeach;
                    wp_reset_postdata();
    
                endif; // ( $posts )
            ?>
            </td>
            <td>
                <a href="<?php the_sub_field('scheda_tecnica')?>" class="notifiche" data-tipo="Scheda Tecnica">Scarica la Scheda Tecnica</a>
            </td>
            <td>
                <a href="<?php the_sub_field('scheda_di_sicurezza')?>" class="notifiche" data-tipo="Scheda di Sicurezza">Scarica la Scheda di Sicurezza</a>
            </td>
        </tr>
    
        <?php endwhile; // has_sub_field( 'schede_prodotti' ) ?>
    </table>
    <?php endif; // get_field( 'schede_prodotti' ) ?>

    You’ll find that I updated two lines:
    https://gist.github.com/neverything/6573468#file-acf-php-L17 and the more important one: https://gist.github.com/neverything/6573468#file-acf-php-L32

    Please let me know if this works for you and if not, please tell me so I’ll gladly have a look at your WordPress installation. Attached you find a screenshot of the working example table I rendered on my demo site.