WordPress - Trying to split todays posts into morning & afternoon with counter

I have a WordPress function that allows me to displays "Todays" posts on a page and I am trying to take it further to split it into "Morning" and "Afternoon".

Here is the code I am using for Todays Posts:

function my_posts_where_from_today( $where = '' ) {
$where .= " AND post_date > '" . date('Y-m-d', strtotime('now')) . "'";
return $where;
}

This sits in functions.php and I call the filter within the post.

I am also using ordinal suffixes that are dynamically generated as shown in this question:

Add Ordinal Suffix to WordPress Counter

In order to split my posts into morning/afternoon, here is what I am doing:

<?php if(have_posts()): $counter = 1;?>
<?php query_posts('post_type=rushmoor&meta_key=subaru_driver_best_lap&orderby=meta_value_num&order=asc');?>
<?php while(have_posts()):the_post();?>
<?php $driver_best_lap = get_post_meta( get_the_ID(), 'subaru_driver_best_lap', true );?>
<?php $gmt_timestamp = get_post_time('H'); ?>
<?php if ($gmt_timestamp < 12) {?>
<li>
  <div class="rank"><?php echo $counter;?><?php echo ordinal_suffix($counter);?></div>
  <div class="name">
    <?php the_title();?>
  </div>
  <div class="lap-time"><?php echo $driver_best_lap;?></div>
</li>
<?php }?>       
<?php $counter++; endwhile;?>
<?php wp_reset_query(); ?>
<?php endif;?>

Afternoon:

<?php if(have_posts()): $counter = 1;?>
<?php query_posts('post_type=rushmoor&meta_key=subaru_driver_best_lap&orderby=meta_value_num&order=asc');?>
<?php while(have_posts()):the_post();?>
<?php $driver_best_lap = get_post_meta( get_the_ID(), 'subaru_driver_best_lap', true );?>
<?php $gmt_timestamp = get_post_time('H'); ?>
<?php if ($gmt_timestamp > 12 && $gmt_timestamp < 24) {?>
<li>
  <div class="rank"><?php echo $counter;?><?php echo ordinal_suffix($counter);?></div>
  <div class="name">
    <?php the_title();?>
  </div>
  <div class="lap-time"><?php echo $driver_best_lap;?></div>
</li>
<?php }?>       
<?php $counter++; endwhile;?>
<?php wp_reset_query(); ?>
<?php endif;?>

The problem is that my counter is getting messed up.

See the following links for examples:

http://www.subarurallyexperience.co.uk/rushmoor/ranking/today/ (shows all todays posts perfectly) http://www.subarurallyexperience.co.uk/rushmoor/ranking/today/morning/ (shows only morning posts, but counter is messed up) http://www.subarurallyexperience.co.uk/rushmoor/ranking/today/afternoon/ (shows only afternoon posts, but counter is messed up)

I have been trying for like an hour to fix the counter, but I cannot seem to get it working.

Would appreciate any help as this is doing my head in!

Thanks

Solutions

You need to place the part where you increment the counter in the if loop:

  ...
</li>
<?php $counter++; ?>
<?php } ?>       
<?php endwhile; ?>
Tags: PHP / Mysql / Wordpress

Similar questions

Showing upcoming events (including todays event)?
I have the following loop that doesn't quite work. I want to only show upcoming events including events that are taking place today. At the moment it shows all the upcoming posts but also the posts before todays date. Where am I going wrong? I'll give this a go:
Using get_posts to get a product closest to todays date, with a date meta - not working
Good Morning, I am currently trying to get a fooevents product that is closest to todays date, and of a certain product category. I'm using the following code: However this is seemingly pulling through a random product/event. Can anybody see if i have written anything wrong? The pull through of only the category items is fine, it's just not getting...
PHP date greater than todays date not working
I have looked all over and I can't work out why this isn't working. I am trying to compare today's date to a date I set. I want to know if the date is greater than or equal to todays date. For some reason my code isn't working and I can't figure out why. This is the code I have: I am sure it is something simple but I have looked all over and I can'...
Trying to split a query into sections of six
I have a query which returns all custom post, I'm trying to split all post into sections of six then wrap them in an list item. Right now its performing as it should but breaks when it get to the 12 post, their are a total of 40 post. Im not sure what wrong could someone help.
PHP
Trying to find a good plugin or a way to add counter//share buttons to posts(in search or category page or homepage)
I am trying to find a good (wp)plugin or a code to add social share/counter buttons under my posts. I've tried many of them so far none of them had fb,twitter,google+,linkedin which worked properly. The closes i had was a plugin that have everything aligned correctly, have all the buttons. but when you click on FB(which is the main one IMO) the win...
How to pull posts into two columns with different image sizes using $counter variable?
I have a custom module pulling one specific category with 6 posts displaying the featured image into 2 columns. I like the first article of the top of each column to display a large image as shown in the image below: To mention that I have the necessary CSS code for the first and second column but do not know how to properly use the $counter variab...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.