Support

Account

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

Helping

Date Picker : show posts from only current month

  • Hello,
    Actually I show all my posts with date picker on an event page.

    But I would like to display posts from only current month :
    -> all posts with January on Date Picker must be display this month,
    -> all posts with February on Date Picker will be display next month,
    -> etc. …

    It could be something like that ?

    <?php
        $current_month = date('M');
        if($current_month == Jan) {
            echo "display post";
        } else {
            echo "don't display";
        }
    
        if($current_month == Feb) {
            echo "display post";
        } else {
            echo "don't display";
        }
    
        // etc. ...
    ?>

    Thanx !

  • 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; ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Date Picker : show posts from only current month’ is closed to new replies.