0

I have several date queries on an events page that outputs events posts according to the month and the year of the post. Each post has a custom date field in the form YYYY-mm-dd and a corresponding custom field for year and month. The months on the events page are on a 11 month cycle starting from the current month with tabs allowing the user to look at each month for 10 months. The problem is that sometimes some of the months(usually 2 tabs) are a year behind what its supposed to be. Its hard to point out the which tab is the problem because they are random each month and the tab on either side of the tab that's incorrect is actually correct. Its hard to explain but below is my code showing my php that works out the year and the corresponding get_posts query that finds the data needed to be output.

<!-- Tabs  for current month -->
<?php
$fullDate1 = date('Ym01'); 
$dateM=date('m',strtotime($fullDate1."+6 months"));
$dateY = date('Y');
switch($dateM)
{
    case 01:
        $dateY = date('Y')+1;
        break;

    case 02:
        $dateY = date('Y')+1;
        break;

    case 03:        
        $dateY=date('Y')+1;
        break;

    case 04:
        $dateY=date('Y')+1;
        break;

    case 05:        
        $dateY=date('Y')+1;
        break;

    case 06:
        $dateY=date('Y')+1;
        break;

    default:
        $dateY = date('Y'); 
}
$args = array('post_type' => 'events', 'numberposts' => '-1', 'meta_key' => 'event_start', 'orderby' => 'meta_value_num', 'order' => 'ASC',
               'meta_query' => array( 'relation' => 'AND',
                               array('key'=>'Event Month Start', 'value' => $dateM, 'compare' => '=', 'type' => 'NUMERIC'),
                              array ('key'=>'Event Year Start', 'value' => $dateY, 'compare' => '=', 'type' => 'NUMERIC')
                              ));
$recent_posts = get_posts($args); 
foreach($recent_posts as $recent): setup_postdata($recent);
$image = wp_get_attachment_image(get_post_thumbnail_id($recent->ID),'events-loop');
$event_start_S = get_post_meta($recent->ID,'event_start',true);
$event_start = date('j F Y', strtotime($event_start_S));
$event_end_S = get_post_meta($recent->ID,'event_end',true);
$event_end = date('j F Y', strtotime($event_end_S));?>

                <article>
                  <div class="pic"><a href="<?php echo get_the_permalink($recent);?>" class="w_hover img-link img-wrap"><?php echo $image; ?><span class="link-gallery"></span> </a> </div>
                  <h3><a href="<?php echo the_permalink($recent); ?>"><?php echo get_the_title($recent); ?></a></h3>
                  <ul class="icons">
                    <li><a href="<?php echo get_the_permalink($recent);?>" class="post_date"><?php echo $event_start;?> - <?php echo $event_end;?></a></li>
                    <li><a href="" class="comments_count"></a></li>
                  </ul>
                   <div class="text"><?php echo get_excerpt(900,$recent); ?></div>
                </article>

<?php endforeach;
wp_reset_postdata(); ?>

The code above puts the tab 6 months ahead of the current month then checks to see if it should be next year or not. The query is then executed then the data is output. From what I can tell for some reason the year isn't pushed to next year even though the case statement says it should add a year. Any help will be appreciated. Thanks

2

date returns a string but you are using integers in the switch statement. Use:

case '01':
    $dateY = date('Y')+1;
    break;

etc.

However, the switch code can be much simpler, and there is no need to call the date function again since dateY has already been initialised in advance:

switch($dateM)
{
  case '01':
  case '02':
  case '03':
  case '04':
  case '05':
  case '06':
    $dateY+=1;
}

And even simpler, you can skip the switch altogether:

if(intval($dateM) < 7) $dateY+=1;
  • Thanks that did it!! I dont understand why it works the old way with some months but not all. Thanks again – Zayd Bhyat Dec 5 '17 at 7:45
  • yeah, that's strange, technically, comparing e.g. 01 (octal 1) is equal to '01' when doing loose comparisons as is done in switch statements. please accept my answer if it has solved your problem :) – inarilo Dec 5 '17 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.