Support

Account

Home Forums ACF PRO Date Picker : show posts from only current month Reply To: Date Picker : show posts from only current month

  • Fixed and dynamic 🙂 !

    <?php
    
    $year = date('Y');
    $month = date('m');
    //echo 'Current Year : '.$year.'<br />';
    //echo 'Current Month : '.$month.'<br />';
    
    $posts = get_posts(array(
            'posts_per_page'                => -1,
            'meta_query'                    => array(
                    'relation' => 'AND',
                    array(
                            'key' => 'calendrier',
                            'value' => $year.'-'.$month.'-01',
                            'compare' => '>=',
                    ),
                    array(
                            'key' => 'calendrier',
                            'value' => $year.'-'.$month.'-31',
                            'compare' => '<=',
                    )       
            ),
            'order'                         => 'ASC',
            'orderby'                       => 'meta_value',
            'meta_key'                      => 'calendrier',
    ));
    
    if( $posts ): ?>
    
            <?php foreach( $posts as $p ): ?>
                    <article>
                            <p><?php the_field('calendrier', $p->ID); ?></p>
                            <p><?php echo $p->post_title; ?></p>
                    </article>
            <?php endforeach; ?>
    
    <?php endif; ?>