is this WordPress post the latest in category

Recently I needed a special layout for the latest WordPress post in each category so I had to find out if the active post is the latest post in a specified category.

I found an is_latest function somewhere in Google and extended it to the is_latest_in_category():

function is_latest_in_category($post,$category) {
          if (!in_category($category,$post))
              return False;

          global $wpdb;
          $sql = sprintf("SELECT COUNT(*)
                            FROM %s p
                            INNER JOIN %s rel ON p.ID = rel.object_id
                            INNER JOIN %s cat ON rel.term_taxonomy_id=cat.term_id
                            WHERE post_date > '%s'
                                AND post_type = 'post'
                                AND post_status = 'publish'
                                AND cat.slug='%s'
                                ;",
                  $wpdb->posts,
                  $wpdb->term_relationships,
                  $wpdb->terms,
                  $post->post_date,
                  $category
          );
          return $wpdb->get_var($wpdb->prepare($sql)) == 0;
        }

Add this function to your themes functions.php and use it anywhere in your theme

if ( is_latest($post,'my_category_slug') ){...}

Add this function to your themes functions.php and use it anywhere in your theme

if ( is_latest($post,'my_category_slug') ){...}