Support

Account

Forum Replies Created

  • 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 ‘

    • Du’ . $event_start_date . ‘ au ‘ . $event_end_date . ‘
    • ‘;

      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 ??

  • i saw, yes, facet seems to be a very great plugin !!and much more simple than using acf with custom filters!

  • Hello, no i don’t. I just use acf (free version).you can use facet with acf map??
    i’d be curious to see the result!! : )

Viewing 3 posts - 1 through 3 (of 3 total)