Finally found out using two (single) posts metas :
- likes_log where I keep up to date an array where each entry is the time the item was liked. Entries are removed if they are too old (here, >1 month)
-
likes_log_count where I update the number of entries in the previous log.
function update_likes_monthly_count(){ if ( get_post_status($this->post_id) != 'publish') return; $log = array(); $time = current_time( 'timestamp' ); $time_remove = strtotime('-1 month',$time); if ($existing_log = get_post_meta($this->post_id, 'likes_log', true)){ //get month log $log = $existing_log; } //remove entries that are too old from log metas (multiple) foreach ((array)$log as $key=>$log_time){ if ($log_time <= $time_remove){ unset($log[$key]); } } //update log $log[] = $time; update_post_meta($this->post_id, 'likes_log', $log); //update likes count $count = count($log); return update_post_meta($this->post_id, 'likes_log_count', $count ); }
then, I can use ‘likes_log_count to filter my query !
$query->set('meta_key', 'likes_log_count' );
$query->set('orderby','meta_value_num');
$query->set('order', 'DESC');