Home › Forums › General Issues › ACF PRO – Date and compare (merge)
Hello I am trying get all date and name of events. I need get this result:
22 JUL
EVENT 1 EVENT 2
25 JUL
EVENT 3
30 JUL
EVENT 4 EVENT 5
I use something like this (condition in foreach is nonsense):
<?php
$posts = get_posts(array(
'post_type' => array('town1_event', 'town2_event'),
'meta_key' => 'datum_eventu',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
if( $posts ) {
foreach( $posts as $post ) {
$nazev = get_field("nazev_eventu");
$date = get_field('datum_eventu', false, false);
$date = new DateTime($date);
?>
<?php
if($date->format('j M') == $date->format('j M')){
?>
<div class="calendar_head">
<span><?php echo $nazev?></span><br/>
<span><?php echo $date->format('j M');?></span>
</div>
<?php
}else{
echo "<br/>";
echo $date->format('j M')
}
}
wp_reset_postdata();
}
?>
Is there any possibility how compare date from town1 and town2 and get it to one div ?
Thank you very much !
Hello I solved it
$the_query = new WP_Query( array(
'post_type' => array('town1_event','town1_event2'),
'post_status' => 'publish',
'meta_key' => 'datum_eventu',
'orderby' => 'meta_value_num',
'order' => 'ASC'
) );
# This will hold what group we're in
$current_header = '';
# The Loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
# get the datum for this post
$temp_date = get_post_meta( get_the_ID(), 'datum_eventu', true );
# If they aren't the same, we'll start a new group, which for now
# just means setting a new heading
if ( $temp_date != $current_header ) {
$current_header = $temp_date;
$old_date = date($current_header); // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('d.m.Y', $old_date_timestamp);
echo "<h2>$new_date</h2>";
}
$nazev = get_field("nazev_eventu");
echo $nazev;
echo "<br/>";
# ... do normal loop stuff here
endwhile;
Inspired on stack over flow
The topic ‘ACF PRO – Date and compare (merge)’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.