retrieving tags and counts from WordPress blog

A website I’ve been working on has a WordPress blog included in it. The site is not using WordPress as the CMS, just as one section. While the blog is sub-foldered (e.g. http://%5BSITENAME%5D/blog/) there was still a need to refer to the tags elsewhere on the website, and link to them. While it would definitely be possible to reference components of the blog itself, and then call the function/s to get tags and counts etc, I decided to hook into the DB with some SQL and rock it from there.

After a quick explore of the WordPress DB, I pulled the tables and fields I needed to reference in order to get the results I desired. The query itself is pretty straight-forward:

SELECT wp_term_taxonomy.count AS count, wp_terms.name AS name 
FROM wp_term_taxonomy, wp_terms 
WHERE wp_term_taxonomy.term_taxonomy_id = wp_terms.term_id 
AND wp_term_taxonomy.taxonomy =  'post_tag';

Walking through this query step-by-step we have:

1 – SELECT wp_term_taxonomy.count AS count, wp_terms.name AS name
choose the values we want, and from which tables, and give them an alias
2 – FROM wp_term_taxonomy, wp_terms
define the tables we are referencing during this query
3 – WHERE wp_term_taxonomy.term_taxonomy_id = wp_terms.term_id
AND wp_term_taxonomy.taxonomy = 'post_tag';
put conditions to the results; here we want to make sure we are referring to the same term, and that it is a term of type ‘post_tag’

Implementation is very simple, the sample below is a delivery in PHP:

// connect to your DB
$sql = "SELECT wp_term_taxonomy.count AS count, wp_terms.name AS name ".
"FROM wp_term_taxonomy, wp_terms ".
"WHERE wp_term_taxonomy.term_taxonomy_id = wp_terms.term_id ".
"AND wp_term_taxonomy.taxonomy =  'post_tag';";
$taglinks = "";
$result = @mysql_query($sql);
if($result) {
	while($row = mysql_fetch_array($result)) {
		$tag = $row['name'];
		$count = $row['count'];
		$taglinks .= "<a href='http://[SITENAME]/blog/?tag={$tag}' 
				class='tag-link-{$count}' title='{$tag}' >
				{$tag}</a><br /> ";
	}
	// close the DB connection for security, e.g.
	// mysql_close([CONNECTION]);
}

In my example, I have retrieved the tags and their counts and formatted them into absolute links which are styled based on the number of uses. This enables them to be displayed more prominently based on popularity for example.

Advertisements

About Cameron
I'm a final year Computer Science/Information Systems major. Already finished my BA in Politics/Philosophy. I do web and software freelance on the side, while I finish studying. Hoping to be self-employed by the end of my degree, otherwise off into the real-world I go....

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: