Support

Account

Home Forums General Issues How to filter posts by event month

Solving

How to filter posts by event month

  • I would like to add filtering option to my posts, where they can be filtered by event date. That event date is a custom field (date picker). I would like to create a list of months only, where by clicking on January it only shows events in that month (not events/posts created in that month, but posts containing the custom field with January)
    Here is my code so far for all posts:

    
        <?
        /*
        *  Order Posts based on Date Picker value
        *  this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP)
        */
        $args = array(
            'meta_key' => 'event_date', // name of custom field
            'orderby' => 'meta_value_num',
            'order' => 'DSC',
            'cat' => '2'
        );
    
        $the_query = new WP_Query( $args );	
             if ( $the_query->have_posts() ) {
                while ( $the_query->have_posts() ) { 
        ?>			
                <?php $the_query->the_post(); ?>
    
                        <li id="post-<?php the_ID(); ?>">
                                <?php 
                                    $date = get_field('event_date');
                                    $dateformat = date("F j, Y", strtotime($date));
                                ?>
                                <p class="date"><?php echo $dateformat; ?> </p>
                                <h4 <?php the_title(); ?></h4>
                                <a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'thumb' ); ?></a>
                                <p><?php the_tags( '', ', ', ''); ?></p>
                        </li>
    
            <?php } } else { ?>
                <h2>Not Found</h2>
            <?php //endif; ?>
        <?php } ?>
        <?php wp_reset_postdata(); ?>
  • Dates are stored in the format YYYYMMDD. To get only posts for a specific month, lets say July of 2015. You would need to dynamically insert the year and month into the following for whatever month you need.

    
        $args = array(
            'meta_key' => 'event_date', // name of custom field
            'orderby' => 'meta_value_num',
            'order' => 'DSC',
            'cat' => '2'
            'meta_query' => array(
                'relation' => 'AND',
                array(
                    'key' => 'event_date',
                    'value' => '20150701',
                    'compare' => '>=',
                ),
                array(
                    'key' => 'event_date',
                    'value' => '20150731',
                    'compare' => '<=',
                ),
            ),
        );
    
  • Thank you for reply!
    But does it mean that if I have events over last two years, I would need to duplicate this code 24 times, to create start and end array for every single month… ? And also every month add new one to the list?
    Is there a way to make this automatic? So that every month new month shows up on that filter?

  • I would think of something dynamic. How are you setting the month? I was assuming this was one query based on the link parameters or something else where month and year would be determined before the query.

    
    $month = 'some code to set month';
    $year = 'some code to set year';
    

    then in the query

    
    
            'meta_query' => array(
                'relation' => 'AND',
                array(
                    'key' => 'event_date',
                    'value' => $year.$month.'01',
                    'compare' => '>=',
                ),
                array(
                    'key' => 'event_date',
                    'value' => $year.$month.'31',
                    'compare' => '<=',
                ),
            ),
    
  • Thanks for reply, I dropped it but now I have to resolve the issue.
    Basically I have a date picker custom field where user picks the event date when creating new post. I display all posts on a page and I order them by this event date. What I need is a filter option above the posts where you can pick the month to see only the events happening that month.

  • What you are looking for is that you need to create query variables in the URL and then filter the posts by these query variables. While you’re using ACF to create and display the custom fields you’re problem really isn’t with ACF.

    You are either going to need a form that submits the filter values or you will need to generate URL that include query strings with the the values. Then you need to get the values from $_GET and generate the QP_Query using those values.

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

The topic ‘How to filter posts by event month’ is closed to new replies.