Support

Account

Home Forums Add-ons Repeater Field Calendar by month with date repeater

Solving

Calendar by month with date repeater

  • I’ve create a post type “events”. With ACF plugin and a repeater fucntion my post have a calendar.

    view repeater

    Now, i want to make a calendar page with my events show per month.

    ex :

    July :
    event 1 + all date
    event 2 + all date

    August :
    event 3 + all date
    event 1 + all date

    How to browse all the months of the year with a planned event?

    my actuatly code :

    <?php
    /**
     * Template Name: Calendrier
     */
    ?>
    
    <?php get_header(); 
    $mois_annee = array(
        'juillet',
        'septembre',
    );
    
    setlocale (LC_TIME, 'fr_FR');
    $mois_calendar = strftime('%B');
    $query_calendrier = new WP_Query( array( 
                            'post_type' => 'spectacle' ,
                            'tax_query' => array(
                                'relation' => 'OR',
                                    array(
                                        'cat' => '23',
                                    )
                            )
                        ));
    $has_thumb = has_post_thumbnail();
                    
    ?>
    <?php if ( $has_thumb ) : ?>
        <div class="image-une" data-parallax="scroll" data-image-src="<?php the_post_thumbnail_url(); ?>" data-speed="0.4"></div>
     <?php endif ?>
        <div class="global-content">
            <h1 class="title-page <?php if ( !$has_thumb ) { ?>no-thumb<?php } ?>">
                <?php the_title(); ?>
            </h1>
                        
                <?php foreach ($mois_annee as $mois): ?>
                    <div class="panel-group" id="accordion-calendrier" role="tablist" aria-multiselectable="true">
                        <div class="panel-heading container" role="tab" id="head-<?php echo $mois; ?>">
                            <a role="button" data-toggle="collapse" data-parent="#accordion-calendrier" href="#collapse-<?php echo $mois; ?>" aria-expanded="true" aria-controls="collapse-<?php echo $mois; ?>">
                                <h1><?php echo $mois; ?></h1>
                            </a>
                        </div>
                        
                        <?php $previous = null ?>
    
                        <div id="collapse-<?php echo $mois; ?>" class="collapse in clearfix collapse-content" role="tabpanel" aria-labelledby="heading-<?php echo $mois; ?>">
                            <?php 
                            if ( $query_calendrier->have_posts() ) : while ( $query_calendrier->have_posts() ) : $query_calendrier->the_post(); ?>
    
                                <?php if( have_rows('calendrier') ) : ?>
                                    <?php while ( have_rows('calendrier') ) : the_row();
    
                                        $date_debut = get_sub_field('jour_debut');
                                        $date_fin = get_sub_field('jour_fin');
                                        $lien_map = get_sub_field('lien_map');
                                        $descr_spect = get_sub_field('description-date');
                                        $mois_spectacle = explode(" ", $date_debut)[1];
                                        $jour1_periode = explode(" ", $date_debut)[0];
                                        $jour2_periode = explode(" ", $date_fin)[0];
                                        
                                        if($mois_spectacle == $mois){ ?>
                                            
                                                
                                            <div class="col-xs-12 col-sm-6">
                                                <?php if (get_the_title() != $previous) : ?>
                                                    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><h2><?php the_title(); ?></h2></a>
                                                <?php endif; ?>
                                                    
                                                <?php $previous = get_the_title(); ?>
                                                <div class="infos-date-spectacle">
                                                    <div class="date-spectacle">
                                                        <?php if( !empty($date_fin) ) : ?>
                                                            Du <span><?php echo $jour1_periode; ?></span> au <span><?php echo $jour2_periode; ?></span> - 
                                                        <?php else : ?>
                                                            Le <span><?php echo $jour1_periode; ?></span> -  
                                                        <?php endif; ?>
                                                    </div>
                                                    <div class="lieu-spectacle">
                                                        <?php if( !empty($lien_map) ): ?>
                                                            <a href="<?php echo $lien_map; ?>" targuet="_blank" title="Spectacle de la compagnie Virvolt ร  <?php the_sub_field('lieu'); ?>">
                                                                <?php the_sub_field('lieu'); ?>
                                                            </a>
                                                        <?php 
                                                        else : 
                                                            the_sub_field('lieu'); 
                                                        endif; 
                                                        ?>
                                                    </div>
                                                    <?php if( !empty($descr_spect) ) : ?>
                                                        <div class="description-date-spectacle">
                                                            <?php echo $descr_spect; ?>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>
                                                <!-- <div class="lien-voir-spectacle">
                                                    <a href="<?php the_permalink(); ?>" title="title">Dรฉcouvrir ce spectacle</a>
                                                </div> -->
                                            </div>
                                        <?php }?>
                                        
                                    <?php endwhile; ?>
                                <?php endif; ?>
                                                  
                            <?php endwhile; endif; ?>
                        </div>
                    </div>                          
                <?php endforeach; ?>
        </div>
    <?php get_footer(); ?>
  • Hi @trainsrenton

    I’m afraid you need to use the wpdb class instead. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/. To learn more about wpdb, please check this page: https://codex.wordpress.org/Class_Reference/wpdb.

    I hope this helps ๐Ÿ™‚

  • wouaou !!!!

    it becomes too complex for me ๐Ÿ™

  • with this request I got it . I do not know what to do.

    there all my time and all my post

    array(5) {
    [0]=>
    object(stdClass)#6665 (4) {
    [“meta_id”]=>
    string(4) “1956”
    [“post_id”]=>
    string(3) “408”
    [“meta_key”]=>
    string(23) “calendrier_0_jour_debut”
    [“meta_value”]=>
    string(8) “20160704”
    }
    [1]=>
    object(stdClass)#6683 (4) {
    [“meta_id”]=>
    string(4) “2183”
    [“post_id”]=>
    string(3) “441”
    [“meta_key”]=>
    string(23) “calendrier_0_jour_debut”
    [“meta_value”]=>
    string(8) “20160910”
    }
    [2]=>
    object(stdClass)#6724 (4) {
    [“meta_id”]=>
    string(4) “2204”
    [“post_id”]=>
    string(3) “442”
    [“meta_key”]=>
    string(23) “calendrier_0_jour_debut”
    [“meta_value”]=>
    string(8) “20160927”
    }
    [3]=>
    object(stdClass)#6723 (4) {
    [“meta_id”]=>
    string(4) “1972”
    [“post_id”]=>
    string(3) “408”
    [“meta_key”]=>
    string(23) “calendrier_1_jour_debut”
    [“meta_value”]=>
    string(8) “20160924”
    }
    [4]=>
    object(stdClass)#6722 (4) {
    [“meta_id”]=>
    string(4) “1990”
    [“post_id”]=>
    string(3) “408”
    [“meta_key”]=>
    string(23) “calendrier_2_jour_debut”
    [“meta_value”]=>
    string(8) “20160709”
    }

  • Hi @trainsrenton

    After that, you can sort the data by using the array_multisort() function. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/. Here’s an example how to do it:

    $rows = ''; // The variable that holds the repeater result
    
    // vars
    $order = array();
    
    // populate order
    foreach( $rows as $i => $row ) {
    	
    	$order[ $i ] = $row['meta_value'];
    	
    }
    
    // multisort
    array_multisort( $order, SORT_ASC, $rows );
    
    $current_month = null;
    foreach( $rows as $row){
        $date = DateTime::createFromFormat('Ymd', $row['meta_value']);
        $month = $date->format('F');
        if( $current_month != $month ){
            echo $month . "\r\n";
            $current_month = $month;
        }
        echo $row['meta_value'] . "\r\n";
    }

    You can also get the post object by using the get_post() function like this:

    echo $row['meta_value'] . "\r\n";
    $thepost = get_post($row['post_id']);
    print_r($thepost);

    I hope this helps ๐Ÿ™‚

  • hi james,

    Thank you for your help.

    here is my current code

    <?php
    /**
     * Template Name: Calendrier
     */
    ?>
    
    <?php get_header(); ?>
    <?php 
    $rows = $wpdb->get_results($wpdb->prepare( 
                "
                SELECT * 
                FROM {$wpdb->prefix}postmeta
                WHERE meta_key LIKE %s
                ",
                'calendrier_%_jour_debut'
            ));
    
    /// var_dump($rows);
    
     /*
      array(5) {
      [0]=>
      object(stdClass)#6662 (4) {
        ["meta_id"]=>
        string(4) "1956"
        ["post_id"]=>
        string(3) "408"
        ["meta_key"]=>
        string(23) "calendrier_0_jour_debut"
        ["meta_value"]=>
        string(8) "20160704"
      }
      [1]=>
      object(stdClass)#6676 (4) {
        ["meta_id"]=>
        string(4) "2183"
        ["post_id"]=>
        string(3) "441"
        ["meta_key"]=>
        string(23) "calendrier_0_jour_debut"
        ["meta_value"]=>
        string(8) "20160910"
      }
      [2]=>
      object(stdClass)#6717 (4) {
        ["meta_id"]=>
        string(4) "2204"
        ["post_id"]=>
        string(3) "442"
        ["meta_key"]=>
        string(23) "calendrier_0_jour_debut"
        ["meta_value"]=>
        string(8) "20160927"
      }
      [3]=>
      object(stdClass)#6716 (4) {
        ["meta_id"]=>
        string(4) "1972"
        ["post_id"]=>
        string(3) "408"
        ["meta_key"]=>
        string(23) "calendrier_1_jour_debut"
        ["meta_value"]=>
        string(8) "20160924"
      }
      [4]=>
      object(stdClass)#6715 (4) {
        ["meta_id"]=>
        string(4) "1990"
        ["post_id"]=>
        string(3) "408"
        ["meta_key"]=>
        string(23) "calendrier_2_jour_debut"
        ["meta_value"]=>
        string(8) "20160709"
      }
    } */
                
    $rows = get_field ('calendrier');
    
    // var_dump($rows); = NULL
    
    // vars
    $order = array();
    
    // populate order
    foreach( $repeater as $i => $row ) {
    	
    	$order[ $i ] = $row['id'];
    	
    }
    
    //var_dump($order);
    /*
    array(0) {
    
    }
     
    */
    
    ?>
    
    <?php get_footer(); ?>

    :s

  • Hi @trainsrenton

    You need to use the $rows variable that holds the wpdb result. Something like this:

    $rows = $wpdb->get_results($wpdb->prepare( 
                "
                SELECT * 
                FROM {$wpdb->prefix}postmeta
                WHERE meta_key LIKE %s
                ",
                'calendrier_%_jour_debut'
            ));
    
    // vars
    $order = array();
    
    // populate order
    foreach( $rows as $i => $row ) {
    	
    	$order[ $i ] = $row['meta_value'];
    	
    }
    
    // multisort
    array_multisort( $order, SORT_ASC, $rows );
    
    // Temporary month variable
    $current_month = null;
    
    // Loop through the returned rows
    foreach( $rows as $row){
        
        // Convert the date string to PHP datetime
        $date = DateTime::createFromFormat('Ymd', $row['meta_value']);
        
        // Get the month
        $month = $date->format('F');
        
        // Echo the months only once
        if( $current_month != $month ){
            echo $month . "\r\n";
            $current_month = $month;
        }
        
        // Show the date string
        echo $row['meta_value'] . "\r\n";
        
        // Get the related post
        $thepost = get_post($row['post_id']);
        
        // Show it if you want
        print_r($thepost);
        
        // Get the event repeater for the related post
        $calendrier = get_field('calendrier', $thepost->ID);
        
        // Show it if you want
        print_r($calendrier);
    }

    Hope this helps ๐Ÿ™‚

  • Hello James

    Thank you very mutch for your help. I decided to change method.

    I have created one page “calendar” with my repeater and i use relationship for the relation with my event.

    my current code is :

    <?php
    /**
     * Template Name: Test Calendrier
     */
    ?>
    
    <?php get_header(); ?>
    <div class="title-page no-thumb">
        <?php
        if( have_rows('calendrier') ) :
            while ( have_rows('calendrier') ) : 
                the_row();
                $date_debut = get_sub_field('jour_debut');
                $mois_spectacle = explode(" ", $date_debut)[1];
                
                echo '<h1>'.$mois_spectacle.'</h1>';
        ?>  
            <?php 
            $posts = get_sub_field('spectacle');
            ?>
                <?php
                foreach( $posts as $post):
                ?>
                    <?php setup_postdata($post); ?>
                    <div>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        <span>Date dรฉbut : <?php echo $date_debut ?></span>
                    </div>
                <?php
                endforeach;
                ?>
            <?php wp_reset_postdata();?>
    
        <?php 
        endwhile;endif;
        ?>
    </div>
    <?php
    get_footer(); ?>

    now i must merge the month and event similar.

    but I do not know how…

  • Hi @trainsrenton

    I’m afraid I don’t understand the new setup.

    Could you please share some screenshots of how the fields are used on the backend and give me an example how you want it to be on the front end?

    Thanks ๐Ÿ™‚

  • hi james

    The screenshot

    Back end Page “calendrier” :
    Back end : page calendar

    Current Front :
    Current front

    Result : (the html / Css / JS are OK)
    result

    thanks

  • Hi @trainsrenton

    In this case, you need to sort the repeater first. Please check this page to learn more about it: https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/. Please keep in mind that you need to change the order based on the date like this:

    $order[ $i ] = $row['jour_debut'];

    To show the month only once, you need to store the month in a variable and check it in the loop like this:

    $current_date = null;
    <?php foreach( $repeater as $i => $row ): ?>
        
        if( $current_date != $row['jour_debut'] ){
            echo '<h1>'.$row['jour_debut'].'</h1>';
            $current_month = $row['jour_debut'];
        }
    
    <?php endforeach; ?>

    I hope this helps ๐Ÿ™‚

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

The topic ‘Calendar by month with date repeater’ is closed to new replies.