Home › Forums › General Issues › Possible to Group By values of custom field › Reply To: Possible to Group By values of custom field
Hello. I found this post which is very useful and allowed me to find a mid solution to my problem.
I’m building a widget, and my goal is to display a list of lessons (custom posts) grouped by cities (custom ACF fields) and then display the corresponding dates (custom ACF fields) like the following:
Paris
from 11/04/2020 to 18 /04/2020
from 12/05/2020 to 20/05/2020
Nice
from 13/06/2020 to 18/06/2020
I was blocked and thanks to your code i managed to do it.But what i’m trying to do is to group by city. If two posts have the same city name, display only once the city name and under that all the corresponding dates. Now i have a list that display each time the city name like this :
Paris
from 11/04/2020 to 18 /04/2020
Paris
from 12/05/2020 to 20/05/2020
Nice
from 13/06/2020 to 18/06/2020
Here is my code. For the last part if i use the loop:
foreach( $cat_posts as $p )
{
it repeats the dates 3 times under each city. So i don’t use it:
`public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( ‘widget_title’, $instance[‘title’] );
$tax_id = absint( $instance[‘tax_id’] ) ;
$niveau_id = absint( $instance[‘niveau_id’] ) ;
$args = array(
‘post_type’ => ‘formation’,
‘posts_per_page’ => $instance[‘number_events’],
‘tax_query’ => array(
‘relation’ => ‘AND’,
array(
‘taxonomy’ => ‘categories-formations’,
‘field’ => ‘id’,
‘terms’ => $tax_id,
),
array(
‘taxonomy’ => ‘niveau-formations’,
‘field’ => ‘id’,
‘terms’ => $niveau_id,
),
),
);
$upcoming_events = new WP_Query($args);
if ( $title ) {
echo $before_title . $title . $after_title;
}
// vars
?>
<ul class=”event_entries”>
<?php
while( $upcoming_events->have_posts() ): $upcoming_events->the_post();
$event_start_date = get_field( ‘date_de_debut’);
$event_end_date = get_field(‘date_de_fin’ );
$event_venue = get_field(‘ville’);
$posts = array();
$sorted = array();
$posts = $upcoming_events->posts;
foreach( $posts as $p )
{
$city = get_field(‘ville’);
// add to $sorted
if( !isset($sorted[ $city ]) )
{
$sorted[ $city ] = array();
}
$sorted[ $city ][] = $p;
}
// now loop through sorted
foreach( $sorted as $city_name => $city_posts )
{
echo ‘<h3>’ . $city_name . ‘</h3>’;
echo ‘
echo ‘
‘;
echo ‘
‘;
}
endwhile;
?>
<?php
wp_reset_query();
echo $after_widget;
}
So my goal is when two posts are in the same city group them under the same city name like this :
Paris
from 11/04/2020 to 18 /04/2020
from 12/05/2020 to 20/05/2020
Nice
from 13/06/2020 to 18/06/2020
Can someone help me to change the loop to get this result ??
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.