Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Limit taxonomy tagcloud by date WordPress

  • SOLVED

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?

Answers (1)

2010-06-04

Oleg Butuzov answers:


<?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');
?>


N. Kerkvliet comments:

Hi Oleg, thanks for your reply.

I inserted the code in my sidebar. And I changed the wp_tag_cloud(array to the one from the first post to select from the 'words' taxonomy. The output however is the same as the one from the first post (all the tags of alltime). Is there anything else I need to change?

<?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( 'taxonomy' => 'words', 'number' => 10, 'format' => 'list', 'exclude' => '1765,11241,1070,8133,66,3588,648,1365,12792,950,668,1279,1986,6675', 'largest' => 8, 'orderby' => 'date', 'order' => 'DESC' ) );

remove_filter('query', 'limit_tags');

?>


Oleg Butuzov comments:

have no idea...

can you replace
return $sql;
by
echo $sql;
return $sql;

this will show us a original query perhaps we need to change sql in different way.

also you can replace RIGHT by INNER


N. Kerkvliet comments:

Made the changes, nothing changed though...

the code now:

<?php

function limit_tags($sql){

global $wpdb;

$pattern = " WHERE ";

$replace = " INNER 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);



echo $sql; return $sql;

}

add_filter('query', 'limit_tags');

wp_tag_cloud( array( 'taxonomy' => 'words', 'number' => 10, 'format' => 'list', 'exclude' => '1765,11241,1070,8133,66,3588,648,1365,12792,950,668,1279,1986,6675', 'largest' => 8, 'orderby' => 'date', 'order' => 'DESC' ) );

remove_filter('query', 'limit_tags');

?>


Oleg Butuzov comments:

and you even don't see the the raw sql query? (echo $sql;)


N. Kerkvliet comments:

Nope, that struck me as well...

You can see them at www.twitmedia.nl (in the right sidebar).


Oleg Butuzov comments:

are you sure you are using the inline sidebar code not widgets?


Oleg Butuzov comments:

thats becaus this code not running. and its posible only if widgets activated.


N. Kerkvliet comments:

Hmm...not too sure what you meant by that.

Copied my sidebar.php below.

<?php global $options;
foreach ($options as $value) {
if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] );
}
} ?>

<div id="sidebar">
<div id="searchform">
<form method="get" action="<?php bloginfo('home'); ?>/">
<input name="s" type="text" class="inputs" id="s" value="<?php echo wp_specialchars($s, 1); ?>" size="10" /><?php wp_dropdown_categories('show_option_all=Overal&orderby=name&order=asc&exclude=9,230,6727,8070,8071,8123,8124'); ?>
<input type="submit" class="go" value="ZOEKEN" />
</form>
</div><!--searchform-->

<!--the twitter widget-->
<div id="twitter-entry">
<?php if ($apa_Twitter == ''){$apa_Twitter = 'mks6804';}?>
<?php require_once(ABSPATH . 'wp-includes/class-snoopy.php');
$tweet = get_option("lasttweet");
$url = 'http://twitter.com/statuses/user_timeline/' .$apa_Twitter. '.json?count=20';
if ($tweet['lastcheck'] < ( mktime() - 60 ) ) {
$snoopy = new Snoopy;
$result = $snoopy->fetch($url);
if ($result) {
$twitterdata = json_decode($snoopy->results,true);
$i = 0;
while ($twitterdata[$i]['in_reply_to_user_id'] != '') {
$i++;
}
$pattern = '/@([a-zA-Z]+)/';
$replace = '<a href="http://twitter.com/'.strtolower('1').'">@1</a>';
$output = preg_replace($pattern,$replace,$twitterdata[$i]["text"]);
$output = make_clickable($output);
$tweet['lastcheck'] = mktime();
$tweet['data'] = $output;
$tweet['rawdata'] = $twitterdata;
$tweet['followers'] = $twitterdata[0]['user']['followers_count'];
update_option('lasttweet',$tweet);
} else {
echo "Twitter API not responding.";
}
} else {
$output = $tweet['data'];
}
echo "";
?>
</div><!--twitter widget-->

<div class="sidebar-row">
<h3>trends (beta)</h3>
<?php wp_tag_cloud( array( 'taxonomy' => 'words', 'number' => 10, 'format' => 'list', 'exclude' => '1765,11241,1070,8133,66,3588,648,1365,12792,950,668,1279,1986,6675', 'largest' => 8, 'orderby' => 'date', 'order' => 'DESC' ) ); ?>

</div>


<div class="sidebar-row">
<h3>trends vandaag(beta)</h3>
<?php

function limit_tags($sql){

global $wpdb;

$pattern = " WHERE ";

$replace = " INNER 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);



echo $sql; return $sql;

}

add_filter('query', 'limit_tags');

wp_tag_cloud( array( 'taxonomy' => 'words', 'number' => 10, 'format' => 'list', 'exclude' => '1765,11241,1070,8133,66,3588,648,1365,12792,950,668,1279,1986,6675', 'largest' => 8, 'orderby' => 'date', 'order' => 'DESC' ) );

remove_filter('query', 'limit_tags');

?>
</div>


<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('sidebar') ) : ?>
<?php endif; ?>

</div><!--sidebar-->


Oleg Butuzov comments:

if yes, all wee need to do - just add filter for the widget front...


Oleg Butuzov comments:

can you mail me ftp access to debug it?
butuzov () made.com.ua


N. Kerkvliet comments:

I've just emailed you the login details.


N. Kerkvliet comments:

For future references; Oleg did a great and fast job. Definetely recommended!