Reputation: 13
I've created custom post in my wordpress website. In this post, I create events with a date in the format YYYY-MM-DD. I then want to display the next 3 events on my website.
I've created the following array:
Array{
[0]=> Array(
[EventDate] => 2014/03/14
[TimestampEvent] => 1394582400
[DeltaEvent] => 22327
)
[1]=> Array(
[EventDate] => 2014/03/15
[TimestampEvent] => 1394582401
[DeltaEvent] => 22328
)
[2]=> Array(
[EventDate] => 2014/03/16
[TimestampEvent] => 1394582402
[DeltaEvent] => 22329
)
}
I would like to:
I've pretty much already sorted the last point. The issue is more about sorting my event.
So far, I've tried the uksort() function, but I couldn't get it to work.
Any ideas?
Edited;
@Bram solution is working great! The only thing is that the structure of the Array $next is :
Array{
[0]=> Array(
[EventDate]=> 2014/03/16
[TimestampEvent] => 1394582402
[DeltaEvent] => 22329
)
}
How do I acces the EventDate keys from the $next array? I've tried $next[0][0] but it surprisingly didn't work.
Re-Edited;
$next[$i][DateEvenement]
I'm nearly done! Now everything is displaying on the website every date is good! The only thing is that the 3 nearest dates display in the order that they've been added in the wordpress. My code is the following:
$my_query = new WP_Query('post_type=event');
while ($my_query->have_posts()) : $my_query->the_post();
for($i=0;$i<count($next);$i++){
if(get_field('date') == $next[$i][DateEvent]){ ?>
<div>
<h2><?php echo the_title(); ?></h2>
<p><?php echo the_content(); ?></p>
</div>
<?php
}
}
endwhile;?>
The issue is probably from my query but I'm unsure on how to fix this.
Thanks for the help!
Re-Re-Edited;
@Myself
I've done it mom! It's working like a charm. I only had to fix my query. This is the code I used:
for($i=0;$i<count($next);$i++){
$args = array('numberposts' => 1,'post_type' => 'evenement','meta_key' => 'date','meta_value' => $next[$i][DateEvenement]);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ){
while ( $the_query->have_posts() ){
$the_query->the_post();
if(get_field('date') == $next[$i][DateEvenement]){ ?>
<div>
<h2><?php echo the_title(); ?></h2>
<p><?php echo the_content(); ?></p>
</div>
<?php
}
}
}
}?>
Upvotes: 1
Views: 52
Reputation: 4532
Untested, but something like this should get you near the answer...
Disregard the event that have already happened;
$new_data = array_filter($data, function ($a) { return $a['TimestampEvent']>time(); });
Sort my future events from the nearest to the furthest;
usort($data, function ($a, $b) {
if ($a['TimestampEvent']==$b['TimestampEvent']) return 0;
return ($a['TimestampEvent'] < $b['TimestampEvent']) ? -1 : 1;
});
Display the next 3 events on my website.
$next = array_slice($data, 0, 3);
Upvotes: 1