Home › Forums › Add-ons › Repeater Field › Orderby title instead of foreach › Reply To: Orderby title instead of foreach
Ok I think I figured out a solution for you to test. First we need to loop through the while( has_sub_field( 'schede_prodotti' ) )
and prepare the data for furhter use in $ids and $files. Next we use these arrays containing the $post->IDs from the relationship field and the $files from the other fields in the repeater and prepare our data to get out.
Here is the code:
<?php if ( get_field( 'schede_prodotti' ) ): ?>
<table class="product-list simple-list">
<?php
$ids = array(); // Empty array for the $post->IDs from the relationship fields
$files = array(); // Empty array for the files from the repeater fields
while( has_sub_field( 'schede_prodotti' ) ):
/**
* Get the IDs
*
* Returns an array of the IDs of the posts
* selected using the Relationship field.
*
* @var array
*/
$id = get_sub_field( 'relazione_prodotti' );
$ids[] = $id[0]; // We need the first array of the return value of the relationship field
/**
* Get the files
*
* Store the files under the $post->ID from the relationship field
* and use the field name for reference.
*
* @var $array
*/
$files[$id[0]]['scheda_tecnica'] = get_sub_field('scheda_tecnica');
$files[$id[0]]['scheda_di_sicurezza'] = get_sub_field('scheda_di_sicurezza');
endwhile; // has_sub_field( 'schede_prodotti' )
if ( $ids ) : // Check if we have $ids from the posts.
/**
* 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' // The custom post type you use
);
/**
* Query the Posts
*
* @var array
*/
$posts = get_posts( $args );
if ( $posts ) : // Check if we have $posts
foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<tr>
<td class="title">
<span><?php the_title(); ?></span>
</td>
<td>
<a href="<?php echo $files[$post->ID]['scheda_tecnica']; ?>" class="notifiche" data-tipo="Scheda Tecnica">Scarica la Scheda Tecnica</a>
</td>
<td>
<a href="<?php echo $files[$post->ID]['scheda_di_sicurezza']; ?>" class="notifiche" data-tipo="Scheda di Sicurezza">Scarica la Scheda di Sicurezza</a>
</td>
</tr><?php
endforeach; // ( $posts as $post )
wp_reset_postdata();
endif; // ( $posts )
endif; // ( $ids )
?>
</table>
<?php endif; // get_field( 'schede_prodotti' ) ?>
And again I updated the Gist for better readability: https://gist.github.com/neverything/6573468
Let me know if this works now with the sorting of the titles.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.