0

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.

1
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)

1
  • This works perfect. Thanks for the quick response as well!
    – Mr.Brown
    Mar 12 '13 at 0: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.