Here I’ve written a SQL query based function for you. You can use it to also for getting the top taxonomy terms based on post date range. Here it is-
function the_dramatist_get_taxonomy_based_on_date_range_and_post_count( $args = array() ) {
global $wpdb;
$default_args = array(
'taxonomy' => 'category',
'start_date' => date('Y-m-d'),
'end_date' => date('Y-m-d', strtotime('tomorrow')),
);
$args = wp_parse_args( $args, $default_args );
$sql = $wpdb->prepare("SELECT {$wpdb->prefix}terms.term_id AS terms,
{$wpdb->prefix}terms.name AS name,
{$wpdb->prefix}terms.slug AS slug,
{$wpdb->prefix}term_taxonomy.taxonomy AS taxonomy,
COUNT({$wpdb->prefix}posts.id) AS posts
FROM {$wpdb->prefix}posts
INNER JOIN {$wpdb->prefix}term_relationships
ON ( {$wpdb->prefix}posts.id = {$wpdb->prefix}term_relationships.object_id )
INNER JOIN {$wpdb->prefix}term_taxonomy
ON ( {$wpdb->prefix}term_relationships.term_taxonomy_id =
{$wpdb->prefix}term_taxonomy.term_taxonomy_id )
INNER JOIN {$wpdb->prefix}terms
ON ( {$wpdb->prefix}term_taxonomy.term_id = {$wpdb->prefix}terms.term_id )
WHERE {$wpdb->prefix}term_taxonomy.taxonomy = '%s'
AND {$wpdb->prefix}posts.post_date BETWEEN '%s' AND '%s'
GROUP BY terms
ORDER BY posts DESC",
array(
$args['taxonomy'],
$args['start_date'] . ' 00:00:00',
$args['end_date'] . ' 00:00:00',
)
);
return $wpdb->get_results($sql);
}
For you now usage you should use it like below-
$team_tax_terms = the_dramatist_get_taxonomy_based_on_date_range_and_post_count(array('taxonomy' => 'team'));
$players_tax_terms = the_dramatist_get_taxonomy_based_on_date_range_and_post_count(array('taxonomy' => 'players'));
Those $team_tax_terms
and $players_tax_terms
will give you the terms term_id
, slug
, name
and post_count
. And it will be ordered by post_count
in descending order, that means the terms with most posts will appear in top. I think you should do a print_r
before using this function. That will make the function return data structure clear to you.
This method is tested. I tested it personally. Hope that helps you.