Home › Forums › General Issues › Loop in a loop
Hi,
I have a membership taxonomy where I have custom fields: name, city, job.
I need to loop all my members but grouped by city, just like here: http://www.quilvestprivateequity.com/team?orderBy=location
I started by making a classic loop in which I make a loop. It’s in my second loop where I do not know how to do the filter: tell WordPress that I want to display only members present in this city.
I would especially like it to be dynamic, this way if my client has to add cities in ACF, I do not need to add filters by hand.
Thanks
<?php
$citylist = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'projects'
));
$citymember = get_posts(array(
// 'posts_per_page' => -1,
'post_type' => 'projects',
'meta_key' => 'whattoputhere?',
'meta_value' => 'whattoputhere?'
));
if( $citylist ): ?>
<ul>
<?php foreach( $citylist as $post ):
setup_postdata( $post );
?>
<li>
<h3><?php the_field('city'); ?></h3>
<?php foreach( $citymember as $post ): ?>
<?php the_field('prenom'); ?> <?php the_field('name'); ?>
<?php the_field('job'); ?>
<?php the_field('city'); ?>
<?php setup_postdata( $post );
?>
<?php endforeach; ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Hi,
Finally my problem is solved:
<?php if($orderBy == 'city' ) { ?>
<?php
function getField($title = '') {
$options = array();
$field_groups = acf_get_field_groups();
foreach ( $field_groups as $group ) {
$fields = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'acf-field',
'orderby' => 'menu_order',
'order' => 'ASC',
'suppress_filters' => true,
'post_parent' => $group['ID'],
'post_status' => 'any',
'update_post_meta_cache' => false
));
foreach ( $fields as $field ) {
if ($title && $title == $field->post_title) {
return $field->post_name;
}
$options[$field->post_name] = $field->post_title;
}
}
return $options;
}
$field = get_field_object(getField('Ville'));
$citylist = $field['choices'];
function getCity($fields) {
$key = $fields['ville']['value'];
$ville = $fields['ville']['choices'][$key];
return $ville;
}
?>
<?php
if( $citylist ): ?>
<ul class="global-members-list row" id="accordionTeamCity">
<?php foreach( $citylist as $key => $city ) : ?>
<li class="col-12">
<h3 class="h3-title"><?php echo $city ?></h3>
<ul class="members-list row">
<?php
$teammember = get_posts(array(
'post_type' => 'members',
'meta_key' => 'ville',
'meta_value' => $key));
foreach ( $teammember as $post ): $counter++ ?>
<li class="col-12 col-xl-4 member-col <?php if (get_field('biographie_gauche') || get_field('biographie_droite')): ?>clickable<?php endif; ?>" data-toggle="collapse" data-target="#collapse<?php echo $counter; ?>" aria-expanded="true" aria-controls="collapse<?php echo $counter; ?>">
<div class="member-item">
<?php $ville = getCity(get_field_objects()); ?>
<?php $photo = get_field('photo'); ?>
<div class="member-photo" style="background-image:url('<?php echo $photo['sizes']['member-photo']; ?>');"></div>
<div class="member-intro">
<h4 class="member-name"><?php the_field('prenom'); ?><br /><?php the_field('nom'); ?></h4>
<?php $metier = get_field('metier'); ?>
<p class="member-job"><?php echo implode(', ', $metier); ?></p>
<?php
$fieldmetier = get_field_object('metier');
$valuemetier = $fieldmetier['value'];
// $metier = $fieldmetier['metier'][ $valuemetier ];
?>
<!-- <p class="member-job"><?php echo $metier; ?></p> -->
<?php
$fieldville = get_field_object('ville');
$valueville = $fieldville['value'];
$ville = $fieldville['choices'][ $valueville ];
?>
<p class="member-city"><?php echo $ville; ?></p>
</div>
<div class="trigger-biography-container">
<?php if( get_field('biographie_gauche') ): ?>
<div class="trigger-biography">
<div class="text"><?php echo $boutonbiographie; ?></div>
<img class="accordion-arrow" src="<?php bloginfo('template_url'); ?>/images/accordion-down-orange.png" alt="" />
<img class="accordion-arrow accordion-arrow-top" src="<?php bloginfo('template_url'); ?>/images/accordion-top-white.png" alt="" />
</div>
<?php endif; ?>
</div>
</div>
<div class="member-biography member-biography-collapse row" id="collapse<?php echo $counter; ?>" aria-labelledby="heading<?php echo $counter; ?>" data-parent="#accordionTeamCity">
<div class="biography-col-left col-12 col-xl-6">
<?php the_field('biographie_gauche'); ?>
</div>
<div class="biography-col-left col-12 col-xl-6">
<?php the_field('biographie_droite'); ?>
</div>
</div>
</li>
<?php setup_postdata( $post ); ?>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php } ?>
The topic ‘Loop in a loop’ is closed to new replies.
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.