A good approach to this would be to first fetch all posts sorted by date, and then group them.
As you state the time
custom field is in the format Y-m-d
, so you can properly sort your posts using orderby => 'meta_value'
(not meta_value_num
, as it currently is, which casts the value to a numeric type).
Next, if you want to output a title for each different date, you can store the last displayed date and output it if the date (and change the stored date) if the next post differs from that date. This would yield something similar to this:
$dategroup = '';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
// Fetch date for current post
$postdate = get_post_meta( get_the_ID(), 'time', true );
// Check if the post date differs from the current date
if ( $postdate != $dategroup ) {
// In that case, this is a new date that hasn't yet been displayed
// So, we output the title...
echo '<h3>' . $postdate . '</h3>';
// ...and change the stored date for the current group
$dategroup = $postdate;
[your output stuff]
}
}
}