1. Home
  2. ReHub Theme
  3. Advanced tips and customizations
  4. How to automatically add badge based on post views

How to automatically add badge based on post views

All posts and products can have custom badges. You can configure them in theme option

Also, all posts in our theme have post view counter. Make sure that you didn’t disable it in theme option – global enable/disable.

You can automatically assign badge to posts with views more than special number. This is example of such snippet

add_action('wp_head', 'set_badge_view_counter');
function set_badge_view_counter(){
   global $post;
   $postid = $post->ID;
   if(is_singular()){
   	$views = get_post_meta($postid, 'rehub_views', true);
   	if($views > 1000){
   	        $badge = get_post_meta($postid, 'is_editor_choice', true);
   		if($badge =='' || $badge =='0' || $badge =='no'){
   			update_post_meta($postid, 'is_editor_choice', '1');
   		}
   	}
   }
}

Place code in functions. How to customize your theme

This will assign Editor’s choice badge (first custom badge) to posts with views more than 1000. You can change this number to your own.

Also, our theme has unique post view counter and it counts also views per day, month, week. So, if you want to use this attribute instead global views, change “rehub_views” to rehub_views_day, rehub_views_mon, rehub_views_year

rehub_views_day counts views per 24 hour, so, you can make posts which reaches special number of views per as featured on site with badge.

All of our post modules can be filtered by post badge, so, you can show only posts with special badge on your homepage