WordPress.org

Ready to get started?Download WordPress

Forums

BuddyPress Docs
[resolved] Unread Docs count (2 posts)

  1. laurahreniucu
    Member
    Posted 8 months ago #

    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/

  2. laurahreniucu
    Member
    Posted 8 months ago #

    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;
    
    }

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.