这里,我为您编写了一个基于SQL查询的函数。您还可以使用它来获取基于发布日期范围的顶级分类术语。给你-
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);
}
对于您现在的用法,您应该如下所示使用它-
$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\'));
那些
$team_tax_terms
和
$players_tax_terms
会给你条件
term_id
,
slug
,
name
和
post_count
. 将由以下人员订购
post_count
按降序排列,这意味着大多数帖子的术语将出现在顶部。我想你应该
print_r
使用此功能之前。这将使函数返回的数据结构更清晰。
对该方法进行了测试。我亲自测试过。希望这对你有帮助。