Last active
December 23, 2015 03:29
-
-
Save neverything/6573468 to your computer and use it in GitHub Desktop.
ACF Support: http://support.advancedcustomfields.com/forums/topic/orderby-title-instead-of-foreach/#post-4211
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ) ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment