Support

Account

Home Forums Add-ons Repeater Field Show events if there's a date for today in a repeater

Solved

Show events if there's a date for today in a repeater

  • I’m struggling wrapping my head around this scenario:

    I have an “event” post type and those “events”, for the topic of this thread, have three key fields:

    • A Date field for the “Start date” (data_inizio_evento)
    • Another Date field for the “End date” (data_fine_evento)
    • A Repeater field with these subfields (tabella_spettacoli):
      • A DATE field for the “Day of the spectacle” – get_sub_field(‘data_spettacolo’)
      • A TIME field for the “Time of the spectacle” – get_sub_field(‘orario_spettacolo’)
      • A “SELECT” field for the room where the spectacle takes place – get_sub_field(‘sala_spettacolo’)

    I’ve created a shortcode for displaying the events for TODAY and it “kind-of” works, i mean, it can select only the events that happens today and show the times of the today’s shows if available:

    <?php
    
    // create shortcode to list Today's events
    add_shortcode( 'eventi-oggi', 'protorob_eventi_oggi' );
    
    function protorob_eventi_oggi( $atts ){
    
        ob_start();
    
        //shortcode attributes
    
        extract(shortcode_atts(array(
            'type' => 'eventi',
            'sala' => '',
            'posts_per_page' => '-1'
        ), $atts));
    
        //query
        date_default_timezone_set( 'Europe/Amsterdam' );
        $oggi = date('Y-m-d');
    
        $oggiOptions = array(
            'post_type' => $type,
            'sala' => $sala,
            'posts_per_page' => $posts_per_page,
            'meta_query'=> array(
                'relation' => 'AND',
                array(
                  'key' => 'data_inizio_evento',
                  'compare' => '<=',
                  'value' => $oggi,
                  'type' => 'DATE'
                ),
                array(
                  'key' => 'data_fine_evento',
                  'compare' => '>=',
                  'value' => $oggi,
                  'type' => 'DATE'
                )
            ),
            'order'             => 'ASC',
            'orderby'           => 'meta_value',
            'meta_key'          => 'data_inizio_evento',
            'meta_type'         => 'DATE'
        );
    
        //
    
        $oggiEventQuery = new WP_Query( $oggiOptions );
    
        // This is for displaying today's date
        $oggiform = ucfirst( date_i18n('l, j F Y') );
        echo '<h2 style="text-align:center;">Programmazione ' . $oggiform . '</h2>';
        
        //If there's events for today, display the events
    
        if ($oggiEventQuery->have_posts()) {
    
            echo '<div class="grid-container grid-parent">';
    
    	    while ($oggiEventQuery->have_posts()): 
    	    //The post:
            $oggiEventQuery->the_post(); 
            //Getting some custom fields fr display
            $periodo = get_field('periodo_evento');
            $tabSpett = get_field('tabella_spettacoli');
            $image = get_field('locandina_evento'); 
            //Printing the stuff
            echo '<div class="grid-33">';
            //Show the thumbnail
            if( !empty($image) ):
                echo '<div class="thumb-spettacolo-oggi">';
                echo '<a href="';
                echo the_permalink();
                echo '">';
                echo '<img src="' . $image['url'] . '"/></a></div>';
            endif;
    
            // The Event title
    
            echo '<h3 class="titolo-evento-oggi">' . the_title('','',false) . '</h3>';
            // Display the spectacles times for that day:
    
            if ( have_rows('tabella_spettacoli') ) :
                while (have_rows('tabella_spettacoli')) :
                    the_row();
    
                    //Row Contents
    
                    $dataSpett = get_sub_field('data_spettacolo');
                    $oraSpett = get_sub_field('orario_spettacolo');
                    $salaSpett = get_sub_field('sala_spettacolo');
    
                    //Display only the times for today
                    if ($dataSpett == $oggi) :
                        echo '<span class="orario-singolo '. strtolower(str_replace(' ', '', $salaSpett)) .'"> ' . $oraSpett . ' - ' . $salaSpett . '</span>';
                    endif;
    
                endwhile;
            endif;
    
            echo '</div>';  
    
        endwhile;
    
        echo "</div>"; 
    
        wp_reset_postdata();
    
        $myvariable = ob_get_clean();
        return $myvariable;
    
        }else{
            echo '<h3>Non ci sono eventi in programma per oggi :-( </h3>';
        }   
    }
    
    ?>

    Now, the problem:

    Using that logic If there’s no spectacles for today they will show the same because I’m querying the posts based on the Start date and the End date.

    Is there a way to include the repeater “tabella_spettacoi” subfield in the query so I can show the events only if they have a subfield with a date/time for today? Thus avoiding showing events without spectacles for today?

    Thanks in advance.

  • OK, I think I’ve found the solution reading point 4. in this page:
    https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    now my code looks like this:

    
    // create shortcode to list Today's events only if I have spectacles for today
    add_shortcode( 'eventi-oggi', 'protorob_eventi_oggi' );
    
    function protorob_eventi_oggi( $atts ){
    
    ob_start();
    
        date_default_timezone_set( 'Europe/Amsterdam' );
        
    
        //shortcode attributes
    
        extract(shortcode_atts(array(
            'type' => 'eventi',
            'sala' => '',
            'posts_per_page' => '-1'
        ), $atts));
    
        // filter
        function protorob_posts_where( $where ) {
        
            $where = str_replace("meta_key = 'tabella_spettacoli_$", "meta_key LIKE 'tabella_spettacoli_%", $where);
            return $where;
        }
    
        add_filter('posts_where', 'protorob_posts_where');
    
        $oggi = date('Y-m-d');
    
        //query
    
        $oggiOptions = array(
            'post_type' => $type,
            'sala' => $sala,
            'posts_per_page' => $posts_per_page,
            'meta_query'=> array(
                'relation' => 'AND',
                array(
                  'key' => 'data_inizio_evento',
                  'compare' => '<=',
                  'value' => $oggi,
                  'type' => 'DATE'
                ),
                array(
                  'key' => 'data_fine_evento',
                  'compare' => '>=',
                  'value' => $oggi,
                  'type' => 'DATE'
                ),
                array(
                  'key' => 'tabella_spettacoli_$_data_spettacolo',
                  'compare' => '=',
                  'value' => $oggi,
                  'type' => 'DATE'
                )
            ),
            'order'             => 'ASC',
            'orderby'           => 'meta_value',
            'meta_key'          => 'data_inizio_evento',
            'meta_type'         => 'DATE'
        );
    
        //
    
        $oggiEventQuery = new WP_Query( $oggiOptions );
    
        // This is for displaying today's date
        $oggiform = ucfirst( date_i18n('l, j F Y') );
        echo '<h2 style="text-align:center;">Programmazione ' . $oggiform . '</h2>';
        
        //If there's events for today, display the events
    
        if ($oggiEventQuery->have_posts()) {
    
            echo '<div class="grid-container grid-parent">';
    
            while ($oggiEventQuery->have_posts()): 
            //The post:
            $oggiEventQuery->the_post(); 
            //Getting some custom fields fr display
            $periodo = get_field('periodo_evento');
            $tabSpett = get_field('tabella_spettacoli');
            $image = get_field('locandina_evento'); 
            //Printing the stuff
            echo '<div class="grid-33">';
            //Show the thumbnail
            if( !empty($image) ):
                echo '<div class="thumb-spettacolo-oggi">';
                echo '<a href="';
                echo the_permalink();
                echo '">';
                echo '<img src="' . $image['url'] . '"/></a></div>';
            endif;
    
            // The Event title
    
            echo '<h3 class="titolo-evento-oggi">' . the_title('','',false) . '</h3>';
            // Display the spectacles times for that day:
    
            if ( have_rows('tabella_spettacoli') ) :
                while (have_rows('tabella_spettacoli')) :
                    the_row();
    
                    //Row Contents
    
                    $dataSpett = get_sub_field('data_spettacolo');
                    $oraSpett = get_sub_field('orario_spettacolo');
                    $salaSpett = get_sub_field('sala_spettacolo');
    
                    //Display only the times for today
                    if ($dataSpett == $oggi) :
                        echo '<span class="orario-singolo '. strtolower(str_replace(' ', '', $salaSpett)) .'"> ' . $oraSpett . ' - ' . $salaSpett . '</span>';
                    endif;
    
                endwhile;
            endif;
    
            echo '</div>';  
    
        endwhile;
    
        echo "</div>"; 
    
        wp_reset_postdata();
    
        $myvariable = ob_get_clean();
        return $myvariable;
    
        }else{
            echo '<h3>Non ci sono eventi in programma per oggi :-( </h3>';
        }     
    }
    

    The only caveat is that I can only use the shortcode once in the same page 🙁

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

The topic ‘Show events if there's a date for today in a repeater’ is closed to new replies.