Wordpress Group Fields raggruppare campi da query
<?php
$args = array(
'post_type' => 'branch',
'numberposts' => -1,
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'state',
);
$branch_posts = get_posts($args);
foreach($branch_posts as $post) :
setup_postdata( $post );
?>
<?php
// Only show posts with the 'State' custom field
if (get_field('state')) {
// Set the state of this post
$current_state = get_field('state');
// If this post doesn't have the same 'State' as the last one, let's give this one a heading and open the <ul>
if ($current_state != $previous_state) {
// $previous_state isn't set until the end of this foreach, so the first one doesn't have this variable
// If this isn't the first list, close the last list's <ul>
if ($previous_state) { echo '</ul>'; }
?>
<h1><strong><?php echo $current_state; ?></strong></h1>
<ul>
<?php } ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php
// Remember, we closed the <ul> before the <li>'s, clever huh?
// Set the 'State' of the this post, to compare to the next post
$previous_state = get_field('state');
} ?>
<?php endforeach; wp_reset_postdata(); ?>
</ul>