Title: Unread Docs count
Last modified: August 21, 2016

---

# Unread Docs count

 *  Resolved [laurahreniucu](https://wordpress.org/support/users/laurahreniucu/)
 * (@laurahreniucu)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/unread-docs-count-1/)
 * Hello.
 * I posted about this before, but that was another scenario (i wanted unread docs
   count for members). For that i updated the post metadata to have a variable called(
   read_by_user_x, where x is the user id) and then queried the database using this
   metadata. Useless to say it works perfect, so thank you Boone for that :).
 * Now i want to get the unread docs count for every group I have. I wrote this 
   function:
 *     ```
       function bp_docs_get_unread_count_group($user_id = false, $group_id) {
   
       	if ( false === $user_id ) {
       		$user_id = bp_loggedin_user_id();
       	}
   
       	$doc_count = 0;
       	$unread_key = 'read_by_user_' . $user_id;
   
       	$docs_args = array(
       		'doc_slug' => '',
       		'group_id' => $group_id,
       		'meta_query' => array(
       		array(
       			'key' => $unread_key,
       			'value' => '1'
       			)
       		)
       	);
   
       	$query = new BP_Docs_Query( $docs_args );
   
       	$query->get_wp_query();
   
       	if ( $query->query->have_posts() ) {
       		$doc_count = $query->query->found_posts;
       	}
   
       	if ( !$doc_count )
       		$doc_count = '0';
   
       	if( $doc_count == 0) {
       		$doc_count = '';
       	}
   
       	return $doc_count;
   
       }
       ```
   
 * But this gives me all the docs in that group, no matter if they are read by the
   user or not. Can anyone give me an idea on how to query the database to get only
   the unread docs?
 * Thanks in advance.
 * [http://wordpress.org/plugins/buddypress-docs/](http://wordpress.org/plugins/buddypress-docs/)

Viewing 1 replies (of 1 total)

 *  Thread Starter [laurahreniucu](https://wordpress.org/support/users/laurahreniucu/)
 * (@laurahreniucu)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/unread-docs-count-1/#post-4259185)
 * I managed to solve this. Here is the code if anyone needs it (i put in the functions.
   php file)
 *     ```
       function bp_docs_get_unread_count_group($user_id = false, $group_id) {
   
       	if ( false === $user_id ) {
       		$user_id = bp_loggedin_user_id();
       	}
   
       	$doc_count = 0;
       	$unread_key = 'read_by_user_' . $user_id;
   
       	$docs_args = array(
       		'doc_slug' => '',
       		'group_id' => $group_id,
       		'posts_per_page' => '-1'
       	);
   
       	$query = new BP_Docs_Query( $docs_args );
       	$query->get_wp_query();
       	if ( $query->query->have_posts() ) {
       		$docs = $query->query->posts;
       	}
   
       	if(count($docs) > 0) {
       		foreach ($docs as $doc) {
       			$meta=get_post_meta( $doc->ID, 'read_by_user_' . $user_id );
       			if($meta[0] == '1' && sizeof($meta) > 0) {
       				$doc_count++;
       			}
       		}
       	}
   
       	if ( !$doc_count )
       		$doc_count = '0';
   
       	if( $doc_count == 0) {
       		$doc_count = '';
       	}
   
       	return $doc_count;
   
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Unread Docs count’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/buddypress-docs.svg)
 * [BuddyPress Docs](https://wordpress.org/plugins/buddypress-docs/)
 * [Support Threads](https://wordpress.org/support/plugin/buddypress-docs/)
 * [Active Topics](https://wordpress.org/support/plugin/buddypress-docs/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/buddypress-docs/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/buddypress-docs/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [laurahreniucu](https://wordpress.org/support/users/laurahreniucu/)
 * Last activity: [12 years, 7 months ago](https://wordpress.org/support/topic/unread-docs-count-1/#post-4259185)
 * Status: resolved