Enough of the old fart nostalgia and facebook whinging posts, it’s time to get back to some coding. I wanted to flex some WordPress muscles, and thought I was biting off on a small, one night bit of updates to the Daily Blank WordPress Theme (a generic thing that runs the DS106 Daily Create site).

Just to set up how the site works, it polls the twitter API once an hour for all tweets that are replies to @ds106dc (that’s why it’s required). Some info is stored locally (as a custom post type, for the WordPress junkies) for each one found, like it’s URL, as well as all hashtags found the tweet (saved as tags) most importantly the one that begins with tdc; these are unique to each Daily Create. For exmaple today’s tag is #tdc1597 and this makes it easy to find it’s URL — http://daily.ds106.us/tdc1597.

So… when you go to that one Daily Create, when the page load, it looks to find how many tweets have been saved with this tag, counts them, and then echoes them out to see using the oEmbed function on the tweet’s URL:


So we can see there are 9 responses so far. But getting this number means a database poll, and it would be nice to be able to show this number elsewhere (see the way it is shown on the front page for the current Daily Create)


as well on the archives listing.

I had done something similar for the DS106 Assignment Bank Theme where each “assignment” / thing stores the number of responses, the number of associated tutorials, and number of views. It’s not too complicated, it means on the template page that displays a single Assignment of Daily Create, I am already running a query to find how many responses there are, so we simply store that in a piece of custom post metadata. And while we are doing that, we get a current value for another field that holds the number of views, we bump it one, and store this new value.

My approach for building this is framed with what I ready somewhere about WordPress (probably from Boone Gorges) that in terms of database/computational “costs”, reading data from the database is much cheaper a process than writing to it.

The work is done mostly in the single.php template.

We get the appropriate tag (store as post metadata), and then run a query to find out how many of the response post types exist with the same tag (e.g. how many tweeted responses for this one daily create)

// get the right tag so we can find the responses
$dailyblank_tag = get_post_meta( $post->ID, 'dailyblank_tag', 1 ); 

// query to get sll responses
$responses_count_query = new WP_Query( 
		'posts_per_page' =>'-1', 
		'post_type' => 'response',
		'hashtags'=> $dailyblank_tag, 

// store total count of all responses
$response_count = $responses_count_query->post_count;
// because grammar
$r_plural = ( $response_count == 1) ? '' : 's';

// update pronto
dailyblank_update_meta( $post->ID, $response_count);

The work then is passed to a common function dailyblank_update_meta() which we pass the database ID for the current post (Daily Create), and the number of responses found. This function does two things – it checks a post metadata field that tracks the number of visits, bumps it one, and updates the value. Then it compares the number it received for a number of responses, and updates the post meta data field if the new value is different.

function dailyblank_update_meta($id, $response_count) {
// update custom post meta to track the views and the number of responses done for each daily
// called on each view of a single daily

	// get current value for the visit count post meta, if it does not exist, start with 0
	$visit_count = ( get_post_meta($id, 'daily_visits', true) ) ? get_post_meta($id, 'daily_visits', true) : 0; 
	//update visit counts
	update_post_meta($id,  'daily_visits', $visit_count);
	// now update the number of responses IF it is more than the current value
	if ( $response_count > get_post_meta($post->ID, 'response_count', 1) ) {
		update_post_meta($id,  'response_count', $response_count);

Now we have information about the number of responses and number of visits associated with each Daily.

Bill Smith asked a legit question

But tracking unique users means keeping track of IP addresses, and while it may have more “meaning” is the overhead worth it? Who cares if someone wants to pump the view count by reloading the page? Occam’s Programming Razor applied here.

It works, but for the DS106 Daily Create, there are more than 200 previous posts that will not get their data setup unless someone happens to visit. I have added a new feature, that could use a nudge to backfill existing content.

I have a low tech approach to this, as I need to sometimes just for trying out new code, testing queries, etc. So the theme has a page template called page-debug.php that has nothing special in it. I use it for one time function calls or checking values. I write some code I need to run, put it inside the template after < ?php the_content?>. I make a dummy page called Debug (or whatever, as long as the shortname/slug is debug, load the updated template, go in my browser to http://myspecial.site/debug and watch it do its work (usually I watch it spawn PHP errors, which I fix and try again). When its done, I remove the code or the entire page-debug.php template.

So…. for the 1/2 a lunatic who might still be reading this post (I write it mainly for my notes, after all), I started keeping these code fragments as GitHub gists, see the code I used to “seed” the values for existing posts.

Basically, it runs through all old posts, it does the query to fund how many responses there up, adds that bit of post meta data, and then, for fun, puts some random number in for a guessed number of visits. Do you see why I love data so much?

Now that I have this data, besides adding it to the display of each Daily Create, I can do more fun stuff. I built a shortcode that allows me to display anywhere in WordPress (page or widget) a list of the Daily Creates with the most responses OR a list of the ones with the most visits. Or heck, both in one page, the new Daily Create Popularity Metrics Display (it is not yet validated on a blockchain ;-)


And I can show that the highest number of responses to a Daily Create is 41, and than happened twice

The shortcode has options to set the number to show, to hide the count, to hide the date, and to show the visits rather than the responses, like on a widget in the footer, I have it set to show the five Daily Create’s with the most responses, and to hide the date, this code is

[topdaily number="5" showdate="0"]

If you are hungry code the code, I got it right here. I find shortcodes insanely useful for getting at information that is sitting there in the database and using it anywhere in WordPress.

function get_daily_tops( $atts ) {

		extract( shortcode_atts( array( "number" => 10,  "type" => 'responses' , "showcount" => 1, "showdate" => 1), $atts ) ); 
		// set metakey to look for known info
		$metakey = (  $type == 'responses' ) ? 'response_count' : 'daily_visits';
		// start empty
		$str = '<ol>';
		// set up arguments to get posts
		$args = array(
			'posts_per_page'   => $number,
			'post_type'        => 'post',
			'post_status'      => 'publish',
			'meta_key'		   =>  $metakey,
			'orderby'		   => 'meta_value_num',

		$myposts = get_posts( $args );
		foreach ( $myposts as $daily ) {
			setup_postdata( $daily );
			$str .= '<li><a href="' . get_permalink($daily->ID) . '">' . 
$daily->post_title . '</a> ';
			if ( $showcount ) $str .=   ' ('. get_post_meta($daily->ID, $metakey, 1) . ') ';
			if ( $showdate ) $str .= get_the_date( get_option( 'date_format' ), $daily->ID);
			$str .=  '</li>';				
		$str .= '</ol>';
		return ( $str );	 

Data. Popular. Hand rolled code.

I wana be more popular than the rest
I’ll do anything anything to impress
Everyone is average but I just wanna be the best
Everything thing I do has to be a fact of interest

Top / Featured Image: Found the direct way, a Google Images search on “popularity” (filtered for licensed to reuse, natch!). I’m not 100% sure of the meaning of the figures at the bottom, so fill in your own meanin, it’ just a metaphor darnit.

This image is a flickr photo by psd https://flickr.com/photos/psd/8597760704 shared under a Creative Commons (BY) license

The post "Puny Data: Popularity Metrics Added to Daily Blank Sites" was originally pulled from under moldy cheese at the back of the fridge at CogDogBlog (https://cogdogblog.com/2016/05/puny-data-popularity-metrics-added-to-daily-blank-sites/) on May 23, 2016.

1 Comment

  • Tony Hirst

    Strikes me again how putting shortcodes together can be classed as a (useful) recreational computing activity: have itch, treat it as a puzzle, puzzle over shortcode to solve it, realise shortcode can be made more general, end up with useful, reusable thing, (learn something along the way). :-)

Leave a Comment

All fields are required. Your email address will not be published.