Using Advanced Custom Fields with the add-on of Date Time Piker, I populate a calendar with the information of Year, Date, Time (am and pm).
On the calendar listing page, I have a query and, for some reason, it skips February and repeats March twice.
Here is the code:
<?php
$today = date('Ymd h:i:s a', time() - 60 * 60 * 24);
#start from current month. Change 2 to however months ahead you want
for ($x=0; $x<=6; $x++) {
$date = new DateTime("$x months");
$date->modify("-" . ($date->format('j')-1) . " days");
#echo $date->format('j, m Y');
$month = $date->format('m');
$year = $date->format('Y');
#echo 'Month= '.$month .' Year= '.$year.' <br>'; #debug
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
WHERE meta_key LIKE %s
AND meta_value LIKE %s
ORDER BY meta_value ASC
",
'show_date_time_%_show_date', // meta_name: $ParentName_$RowNumber_$ChildName
#''.$year.''.$month.'%' // meta_value: 20131031 for example
''.$year.''.$month.'%' // meta_value: 20131031 for example
));
// loop through the results
if( $rows ) {
echo '<div class="month">';
echo '<h2>'.$date->format('F').' '.$date->format('Y').'</h2>';
echo '<ul>';
foreach( $rows as $row ) {
// for each result, find the 'repeater row number' and use it to load the sub field!
preg_match('_([0-9]+)_', $row->meta_key, $matches);
$meta_key = 'show_date_time_' . $matches[0] . '_show_date'; // $matches[0] contains the row number!
// today or later
$showDate = $row->meta_value;
$do_current = ($showDate > $today);
if ( $do_current || $continue ) :
$show_title = get_the_title( $row->post_id );
$machine_name = preg_replace('@[^a-z0-9-]+@','-', strtolower($show_title));
$post_type = get_post_type($row->post_id);
if( $post_type === 'pre-post-show' ){
// echo 'pre-post-show matching';
$postID = $row->post_id;
$posts = get_field('pre_post_related_show', $postID);
if( $posts ){
foreach( $posts as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
$related_show = get_the_title($post->ID);
$related_show_machine = preg_replace('@[^a-z0-9-]+@','-', strtolower($related_show));
endforeach;
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
}
}// post type define
?>
Any thoughts about why this could be happening?