Browse: Home / Snippets /

Hide comments for non-admin users from Assignments/Essays on Dashboard Activity Widget

Contents


Snippet #

Important: All snippets are provided as-is without support or guarantees. These snippets are provided as guidelines for advanced users looking to customize LearnDash. For any additional help or support with these snippets, we recommend reaching out to a LearnDash Expert.

/**
* LearnDash - Hide comments from Assignments and Essay from showing on Dashboard Activity Widget
 */
add_action( 'pre_get_comments', function ( $comment_query ) {
	$current_screen = get_current_screen();
	if ( $current_screen->id == 'dashboard' ) {
		if ( !current_user_can( 'moderate_comments' ) ) {
			$post_query_args = array(
				'post_type'			=>	array('sfwd-assignment', 'sfwd-essays'),
				'post_status'		=>	array( 'draft', 'publish', 'graded', 'not_graded' ),
				'fields'			=>	'ids',
				'nopaging'			=>	true
			);
	
			$post_query = new WP_Query( $post_query_args );
			if ( ( $post_query instanceof WP_Query ) && ( !empty( $post_query->posts ) ) ) {
				$post__not_in = array();

				if ( $comment_query->query_vars['post__not_in'] == '' )
					$post__not_in = $post_query->posts;	
				else {
					$post__not_in = array_merge( (array)$comment_query->query_vars['post__not_in'], $post_query->posts );
				}

				if ( !empty( $post__not_in ) )
					$comment_query->query_vars['post__not_in'] = $post__not_in;
			}
		}
	}
}, 100);