Support » Plugins and Hacks » [Resolved] Unread docs count

[Resolved] Unread docs count

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Boone Gorges


    This is a cool idea, but the concept of “unread” needs a bit of fleshing out. It could mean:
    – Docs that the user has never looked at
    – Docs that have been edited since the last time the user looked at them
    – Docs that have been edited or commented on since the last time the user looked at them

    I’m not sure what makes the most sense from a user’s point of view. Do you have thoughts about it?

    By “unread” i mean docs that the user has never looked at.

    Plugin Author Boone Gorges


    Do you have a sense of where/how this would appear in the UI? Or are you just looking for a function that’ll do it?

    FWIW, at the moment, BP Docs does not currently keep track at all of read/unread status. This’d require storing lots of data: potentially, one piece of data for every Doc-user pair. So I’m wary of including it in Docs itself. But I do think you could build a plugin for this purpose, and I’d be happy to point you in the right direction, if that’s what you’re looking for.

    Ok, i will explain in detail what i want.

    I have a website based on Buddypress and Buddypress Docs. It’s a file sharing system.

    Members can create docs that admins / mods will then read/edit. I need to build some kind of counter to show me how many docs per member has the admin not read yet. And this needs to be showed near the group title and the member name (so, one counter for unread docs for members and anotehr for groups).

    I hope you understand what i’m saying 🙂

    Thank you.

    Plugin Author Boone Gorges


    Yup, that use case definitely makes sense. I’d say that this needs to be built as a separate plugin, because it’s very use-case-specific. Here’s the strategy I’d take:

    – Decide on a good storage mechanism for “read/unread” data. I guess postmeta probably makes the most sense, eg update_post_meta( $doc_id, 'read_by_user_' . $user_id, 1 )
    – When loading a Doc, update that ‘read_by_user_’ key
    – To get your unread counts, do a WP_Query, and use the ‘meta_query’ param to limit to those items that have/don’t have the 'read_by_user_' . $user_id postmeta.
    – Either use hooks or custom templates to display the data

    Good luck! Sounds like a cool project.

    So, when i create the doc, i use update_post_meta to make the key = 1, and then, when i open the doc, use update_post_meta again to make it = 0?

    Or i use it only when loading the Doc.
    I know it may be a dumb question, but, as i said, i’m kind of a noob 😀

    Also, unfortunatelly, i don’t have time to build a new plugin. I want to edit this plugin’s files. It’s ok, since i won’t be upgrading.

    Could you please tell me where should i edit?

    Thank you 🙂

    Plugin Author Boone Gorges


    It really doesn’t take any additional time to build a plugin as opposed to editing the Docs core files. But, it’s your call.

    I’m afraid I can’t give more specific pointers than what I’ve already given above. You’ll have to track how Docs are saved; look into buddypress-docs/includes/query-builder.php to get a preliminary sense.

    Ok, so i managed to get this done. I will post the changes i made, in case anyone needs this.

    1. buddypress-docs\includes\query-builder.php – function “save” – line 556

    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    $user_query = new WP_User_Query( array( 'exclude' => array( $user_id) ) );
    if ( ! empty( $user_query->results ) ) {
    	foreach ( $user_query->results as $user ) {
    		update_post_meta( $this->doc_id, 'read_by_user_' . $user->ID, 1 );

    2. buddypress-docs\includes\templates\docs\docs-header.php – line 9

    <?php $current_user = wp_get_current_user();
    	  $user_id = $current_user->ID;
    <?php update_post_meta( get_the_ID(), 'read_by_user_' . $user_id, 0 ) ?>

    3. buddypress-docs\includes\functions.php – end of file

    function bp_docs_get_unread_count($user_id = false, $author_id) {
    	if ( false === $user_id ) {
    		$user_id = bp_loggedin_user_id();
    	$unread_post_count = 0;
    	$unread_key = 'read_by_user_' . $user_id;
    	$args = array(
    		'author' => $author_id,
    		'post_type' => 'bp_doc',
    		'meta_query' => array(
    				'key' => $unread_key,
    				'value' => '1'
    	$query = new WP_Query( $args );
    	$unread_post_count = $query->found_posts;
    	if( $unread_post_count == 0) {
    		$unread_post_count = '';
    	return $unread_post_count;


    function get_user_id_by_display_name( $display_name ) {
        global $wpdb;
        if ( ! $user = $wpdb->get_row( $wpdb->prepare(
            "SELECT <code>ID</code> FROM $wpdb->users WHERE <code>display_name</code> = %s", $display_name
        ) ) )
            return false;
        return $user->ID;

    4. buddypress\bp-themes\bp-default\members\members-loop.php – line 47

    	$current_user = wp_get_current_user();
    	$user_id = $current_user->ID;
    	$name_id = get_user_id_by_display_name(bp_get_member_name());

    <a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a>
    <a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?><?php echo bp_docs_get_unread_count($user_id, $name)?></a>

    This will result in the total number of unread docs for each user to be listed next to the user name on the “Members” page.

    Thank you Boone for all your answers and especially for your patience with me 🙂

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Resolved] Unread docs count’ is closed to new replies.