WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to be able to add a class such as .samedate to posts that share the same meta_value_num from a custom meta box.

So for example:
Say two posts share the same custom meta box value for a date entered as 2013/11/03... If those two posts have the same meta value number, then add the class to those posts.

share|improve this question
up vote 1 down vote accepted
add_filter('post_class', function($classes){
  global $wp_query;
  static $meta = array();       

  // first call gathers meta values
    foreach($wp_query->posts as $post)
      $meta[$post->ID] = get_post_meta($post->ID, 'your_meta_key', true);

  // $wp_query->post should be the global (current) $post object
  $this_meta = $meta[$wp_query->post->ID];

    $counts = array_count_values($meta);

    if($counts[$this_meta] > 1)
      $classes[] = 'your_class';

  return $classes;


share|improve this answer
This works perfect. Thanks for the quick response as well! – Mr.Brown Mar 12 '13 at 0:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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