Divide query results by "week of" in custom post type (Wordpress / PHP)

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;

// Get event dates from WP metabox data (returns as string) 
$longstartdate = get_post_meta($post->ID, 'tf_events_startdate', true); //get start date meta data
$longenddate = get_post_meta($post->ID, 'tf_events_enddate', true); //get start date meta data
$longweekof = get_post_meta($post->ID, 'tf_events_weekof', true); //get Week Of date meta data

// Reformat dates
$prettystartdate = date("D. M. j, Y", $longstartdate);
$prettyenddate = date("l, F j, Y", $longenddate); 
$prettyweekof = date("m/d", $longweekof);

// Get Custom Meta Terms/Catagories/taxonomies
$the_venue = get_the_term_list(  $post->ID, 'venue' );
$event_age = get_the_term_list(  $post->ID, 'event_age' );

// Get Custom Metabox Data - URL for Buy Tickets
$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 ?>
+3
source share
2 answers

Here is a rude solution that works for me.

<?php
$loop = new WP_Query(array(
'post_type'         => 'tf_events', 
'posts_per_page'    => -1, 
'orderby'           => 'meta_value', 
'meta_key'          => 'tf_events_startdate', 
'order'             => 'ASC'
));

$groups = array();
$heading = '';
if ($loop->have_posts()) {
while ($loop->have_posts()) {
    $loop->the_post();

//set up pretty week
$weekof = get_post_meta($post->ID, 'tf_events_weekof', true); //get Week Of date meta data
$longstartdate = get_post_meta($post->ID, 'tf_events_startdate', true); //get start date meta data
$longenddate = get_post_meta($post->ID, 'tf_events_enddate', true); //get start date meta data

//format dates
$prettyweekof = date("m/d", $weekof);
$prettystartdate = date("D. M. j, Y", $longstartdate);
$prettyenddate = date("l, F j, Y", $longenddate); 

//set info on objects
$post->prettyweekof = $prettyweekof;
$post->pretty_startdate = $prettystartdate;
$post->pretty_enddate = $prettyenddate;

//Echo heading when need to
if($post->prettyweekof !== $heading)
{
$heading = $post->prettyweekof;
echo '<h2>'.$heading.'</h2>';

}

//list events
echo $post->post_title.' | '.$post->pretty_startdate.' - '.$post->pretty_enddate.'<br />';


}
}
?>
0
source

When I need to group elements, I usually use an associative array, where the key is what I need to group. Therefore, in your case, the array will look something like this:

array(
    'Week of 4/30' => array(array of posts)
);

An easy way to do this (once you have all the messages) is as follows:

$groups = array();

foreach ($posts as $post) {
    $groups['Week of ' . date('m/d', $post->pub_date)][] = $post;
}

And then you can simply:

foreach ($groups as $title => $posts) {
     echo "<h2>$title</h2>";

     foreach ($posts as $post) ...
}

: ( WP ..):

<?php
# Get all posts
$loop = new WP_Query(array(
    'post_type'         => 'tf_events', 
    'posts_per_page'    => -1, 
    'orderby'           => 'meta_value', 
    'meta_key'          => 'tf_events_startdate', 
    'order'             => 'ASC'
));
$groups = array();

# Create groups of posts
if ($loop->have_posts) {
    while ($loop->have_posts()) {
        $loop->the_post();

        $groups['Week of ' . date('m/d', $post->tf_events_weekof)][] = $post;
    }
}
?>

<h1>Groups of posts</h1>

<?php foreach ($groups as $week => $rows) : ?>
    <h2><?php echo $week ?></h2>

    <ul>
        <?php foreach ($rows as $post) : setup_postdata($post) ?>
            <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
        <?php endforeach ?>
    </ul>
<?php endforeach ?>
0

All Articles