The example shows that I sort posts with custom field “Start Date”. I am using ‘meta_query’ to query the custom fields – you can find out more here.
$args['post_type'] = array('ur_post', 'programs_post', 'education_post', 'blog_post' ); $args['meta_key'] = 'Start Date'; $args['meta_query'] = array( array( 'key' => 'Start Date', 'value' => date('Ymd'), 'compare' => '>=', 'type' => 'date' ) ); /*This will sort. Make sure there is no underscore. It's orderby NOT order_by.*/ $args['orderby'] = 'meta_value'; $args['order'] = 'asc'; $the_query = new WP_Query($args); while ( $the_query->have_posts() ) { $the_query->the_post(); $start_date = get_post_custom_values('Start Date', $the_query->ID); /*var_dump($custom_values);*/ if (date('Ymd',strtotime($start_date[0])) >= date('Ymd')) { echo '<div class="event_date">'; //display date here... echo '</div>'; echo '<div class="event_post_time">'; $start_time = get_post_custom_values('Start Time', $the_query->ID); echo $start_time[0]; $end_time = get_post_custom_values('End Time', $the_query->ID); if ($end_time != NULL) { echo ' - '.$end_time[0]; } echo '</div>'; } } wp_reset_postdata(); |