0

I have create a custom post type "event" and created some posts in it. I have also created a custom field "EventDate" and giving value like "April 08, 2011", "February 08, 2016" etc for each event post. I have got all event post in fronend. No problem here. Now I want that all event post should be fetched into two section in frontend. The first is "Upcoming Event" and the second is "Past Event". I want to get these result by defining date in custom field "EventDate". If i set a event post custom field value like "February 08, 2016" then this event post should be listed under "Upcoming Events" and if i set a event post custom field value like "April 08, 2011" then this event post should be listed under "Past Events". Here is my code but it is displaying all posts normally.

<?php
  /*
   Template Name: Events
  */
?>
<?php $currentDate = date('F-d-Y'); ?>
<?php $currentDate = strtotime($currentDate) ;?>
<?php
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'EventDate',
'meta_query' => array(
    array(
        'key' => 'EventDate',
        'value' => $currentDate,
        'compare' => '>'
    )
)
 );
 ?>
<?php $futureEvent = new WP_Query( array( 'post_type' => 'event')); ?>
        <div class="event-section">
        <header class="entry-header">
            <h1 class="entry-title"><?php _e( 'Other Upcoming Events', 'twentytwelve' ); ?></h1>
        </header>
        <?php
            if( $futureEvent->have_posts() ) : while( $futureEvent->have_posts() ) : $futureEvent->the_post();
            $eventDate = get_post_meta(get_the_ID(), 'EventDate', true);
            if( $eventDate > $currentDate ){
        ?>
        <div class="event-list">
            <div class="left-event-content">
                <div class="left_thumb">
                    <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail(); ?></a>
                </div>
            </div>
            <div class="right-event-content">
                <div class="event-title">
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </div>
                <div class="event-date-name">
                    <div class="event-date"><?php _e('Date: ', 'twentytwelve'); ?><?php echo get_post_meta(get_the_ID(), 'EventDate', true); ?></div>
                    <div class="event-venue"><?php _e('Venue: ', 'twentytwelve'); ?><?php echo get_post_meta(get_the_ID(), 'wpcf-venue', true); ?></div>
                </div>
            </div>
        </div>
        <?php } endwhile; endif; wp_reset_postdata(); ?>
    </div><!--event section ends-->


    <div class="event-section">
        <header class="entry-header">
            <h1 class="entry-title"><?php _e( 'Past Events', 'twentytwelve' ); ?></h1>
        </header>
        <?php
            if( $pastEvent->have_posts() ) : while( $pastEvent->have_posts() ) : $pastEvent->the_post();
            $eventDate = get_post_meta(get_the_ID(), 'EventDate', true);
        ?>
        <div class="event-list">
            <div class="left-event-content">
                <div class="left_thumb">
                    <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail(); ?></a>
                </div>
            </div>
            <div class="right-event-content">
                <div class="event-title">
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </div>
                <div class="event-date-name">
                    <div class="event-date"><?php _e('Date: ', 'twentytwelve'); ?><?php echo get_post_meta(get_the_ID(), 'EventDate', true); ?></div>
                    <div class="event-venue"><?php _e('Venue: ', 'twentytwelve'); ?><?php echo get_post_meta(get_the_ID(), 'wpcf-venue', true); ?></div>
                </div>
            </div>
        </div>
        <?php endwhile; endif; wp_reset_postdata(); ?>
    </div>

Above I am running two loops with different query. But i am unable to get it. Please see my requirement in screenshot.enter image description here and here is my website link

  • 1
    MySQL doesn't understand your date format, you have to store dates in the numeric format yyyymmdd to query on them. – Milo Feb 9 '15 at 7:06
  • But i am already converting it by strtotime function. Can u explain more things? – Arshad Hussain Feb 9 '15 at 7:08
  • you said your custom fields are in the format "February 08, 2016", are they actually stored as timestamps? – Milo Feb 9 '15 at 7:11
  • but how to use it in loop? will i have to use two loop with different query or anything else? – Arshad Hussain Feb 9 '15 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.