I have a custom type of event message, and I have a custom metabox for the date of the event. All events are added with all dates written to the database in srtotime () UNIX format.
When a list of events is requested, I need to separate them by week. So, "week 4/23" then list the events for this week only. This is followed by "Week 4/30" with events for this week.
All this should be dynamic and logically determine when each week begins, and compare it with the date, which is stored in the database for the returned events, thus grouping them into "weeks".
I hope everything makes sense. I'm just having trouble finding resources for this type of function, and I'm not sure about the necessary logic and what WP might already be using. At the moment, the results are output using the Wordpress user loop, but if necessary, I could use SQL.
Thanks in advance for your help!
* UPDATED CODE
<?php
$loop = new WP_Query(array(
'post_type' => 'tf_events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'tf_events_startdate',
'order' => 'ASC'
));
echo '<pre>';
print_r($loop);
echo '</pre>';
$groups = array();
if ($loop->have_posts()) {
while ($loop->have_posts()) {
$loop->the_post();
$groups['Week of ' . $post->post_date][] = $post;
$longstartdate = get_post_meta($post->ID, 'tf_events_startdate', true);
$longenddate = get_post_meta($post->ID, 'tf_events_enddate', true);
$longweekof = get_post_meta($post->ID, 'tf_events_weekof', true);
$prettystartdate = date("D. M. j, Y", $longstartdate);
$prettyenddate = date("l, F j, Y", $longenddate);
$prettyweekof = date("m/d", $longweekof);
$the_venue = get_the_term_list( $post->ID, 'venue' );
$event_age = get_the_term_list( $post->ID, 'event_age' );
$buytix = get_post_meta( $post->ID, 'buytix', true );
}
}
?>
<h1>Groups of posts</h1>
<?php echo '<pre>'; print_r($groups); echo '</pre>';?>
<?php foreach ($groups as $week => $rows) : ?>
<h2><?php echo $week ?></h2>
<ul>
<?php foreach ($rows as $post) : setup_postdata($post) ?>
<?php echo ' <h1>POST START</h1> <pre>';
print_r($post);
echo '</pre> <h1>POST END</h1>'; ?>
<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a> <?php echo $prettystartdate; ?></li>
<?php endforeach ?>
</ul>
<?php endforeach ?>