Statistische WordPress Functies

  • PHP
  • Snippets
  • Wordpress

In de volgende code staan allerlei functies om statistieken weer te geven. E.g. Hoeveel reacties heb je op je site? Hoeveel pagina’s? Wat is het gemiddelde aantal reacties op mijn berichten etc. Deze code kun je gewoon “includen” of in je functions.php van je huidige thema plakken.

/**
 * WordPress Statistic Functions
 * 
 * @author Atomicon
 */

/**
 * stats_user_count()
 * 
 * Returns the number of users
 * 
 * @return int Number of users
 */
function stats_user_count() {
     global $wpdb;
     return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->users);
}

/**
 * stats_post_count()
 * 
 * Returns the number of published posts
 * 
 * @return int Number of posts
 */
function stats_post_count() {
	global $wpdb;
	return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->posts . ' WHERE post_status = "publish" AND post_type = "post"');
} 

/**
 * stats_page_count()
 * 
 * Returns the number of published pages
 * 
 * @return int Number of pages
 */
function stats_page_count() {
	global $wpdb;
	return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->posts . ' WHERE post_status = "publish" AND post_type = "page"');
}

/**
 * stats_comment_count()
 * 
 * Returns the number of approved comments
 * 
 * @return int Number of approved comments
 */
function stats_comment_count() {
	global $wpdb;
	return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->comments . ' WHERE comment_approved = "1"');
}

/**
 * stats_trackback_count()
 * 
 * Returns the number of trackbacks
 * 
 * @return unt Number of trackbacks
 */
function stats_trackback_count() {
	global $wpdb;
	return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->comments . ' WHERE comment_type = "pingback"');
}

/**
 * stats_avg_comments_per_post()
 * 
 * Returns the average comment count on your posts
 * 
 * @return int Average comment count on posts
 */
function stats_avg_comments_per_post() {
	$comment_count = $this->comment_count();
	$post_count    = $this->post_count();
	if ($post_count) {
		return round($comment_count/$post_count);
	} else {
		return 0;
	}
}

/**
 * stats_category_count()
 * 
 * Returns the number of categories
 * 
 * @return int Number of categories
 */
function stats_category_count() {
	return count(get_all_category_ids());
}

/**
 * stats_tag_count()
 * 
 * Returns the number of tags
 * 
 * @return int Number of tags
 */
function stats_tag_count() {
	global $wpdb;
	return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->terms . ' INNER JOIN ' . $wpdb->term_taxonomy . ' ON ' . $wpdb->terms . '.term_id = ' . $wpdb->term_taxonomy . '.term_id WHERE ' . $wpdb->term_taxonomy . '.taxonomy = "post_tag"');
}

/**
 * stats_link_count()
 * 
 * Returns the number of links
 * 
 * @return int Number of links
 */
function stats_link_count() {
	global $wpdb;
	return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->links . ' WHERE link_visible = "Y"');
}

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *