Horje
Limit taxonomy tagcloud by date - Wordpress Solution
Hello, I'm using the text2tag plugin (this plugin adds all postcontent to a custom taxonomy as tags). I can use this to output the most used tags like this:
<?php wp_tag_cloud( array( 'taxonomy' => 'words', 'number' => 10, 'format' => 'list', 'exclude' => 'X', 'largest' => 8, 'orderby' => 'date', 'order' => 'DESC' ) ); ?>
What I want to achieve is to limit the output by a daterange. So I can find 'trending tags'. F.e. Most popular tags of today* Obama (18 mentions) New York (15 mentions) Iron Man (11 mentions) Robin Hood (7 mentions) *Today should be manually configurable, so I can copy the function for most popular today, this week etc. Anyone who can help with this?

Solution - 1


<?php 
function limit_tags($sql){
	global $wpdb;
	$pattern = " WHERE ";
	$replace = " RIGHT JOIN {$wpdb->prefix}term_relationships AS tr ON ( tr.term_taxonomy_id = tt.term_taxonomy_id AND tr.object_id IN 
					(SELECT ID FROM {$wpdb->posts} WHERE DATE(post_date) >= '2008-08-14' AND DATE(post_date) <= '2011-08-23' )	) ".$pattern;
	$sql = str_replace($pattern, $replace, $sql);
 
	return $sql;
}
add_filter('query', 'limit_tags');
wp_tag_cloud(array('number' => 10));
remove_filter('query', 'limit_tags');
?>
you need to replace dates that i have use for test by your dates, or a result of function [[LINK href="http://php.net/date"]]date[[/LINK]]... or any of [[LINK href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html"]]mysql date functions[[/LINK]]. example of combination for a one day...
<?php 
function limit_tags($sql){
	global $wpdb;
	$pattern = " WHERE ";
	$replace = " RIGHT JOIN {$wpdb->prefix}term_relationships AS tr ON ( tr.term_taxonomy_id = tt.term_taxonomy_id AND tr.object_id IN 
					(SELECT ID FROM {$wpdb->posts} WHERE DATE(post_date) >= '".date("Y-m-d", strtotime("-1 day"))."' AND DATE(post_date) <= NOW() )	) ".$pattern;
	$sql = str_replace($pattern, $replace, $sql);
	 
	return $sql;
}
add_filter('query', 'limit_tags');
wp_tag_cloud(array('number' => 10));
remove_filter('query', 'limit_tags');
?>





Wordpress

Related
Enable comments to be added to a non-blog page - Wordpress Solution Enable comments to be added to a non-blog page - Wordpress Solution
Autofocus+ - Wordpress Solution Autofocus+ - Wordpress Solution
Function to truncate comment text on home page. - Wordpress Solution Function to truncate comment text on home page. - Wordpress Solution
pagination with custom query with permalinks in functions.php - Wordpress Solution pagination with custom query with permalinks in functions.php - Wordpress Solution
Title showing up in body of post - Wordpress Solution Title showing up in body of post - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7