Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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

  // first call gathers meta values
  if(!$meta)
    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];

  if($this_meta){
    $counts = array_count_values($meta);

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

  return $classes;
});

(untested)

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

Your Answer

 
discard

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.