On my taxonomy archive page, I display terms and child terms of the queried taxonomy.
I also want to echo the number of posts associated with each child term. The code below works fine, BUT I want to count only recent/new posts, eg. posted within the last 7 days.
Is it possible to filter $term->count
in any way to achieve this?
$taxonomy = get_query_var('taxonomy');
// get main terms
$terms = get_terms( $taxonomy,
array( 'orderby' => 'name' )
);
// display terms
foreach ( $terms as $term ) {
echo '<h2 class="text-center">' . $term->name . '</h2>';
// display term children as list
$term_id = $term->term_id;
$child_terms = get_term_children( $term_id, $taxonomy );
echo '<ul class="lesson-categories">';
foreach ( $child_terms as $child_term ) {
$term = get_term_by( 'id', $child_term, $taxonomy );
echo '<li>';
echo $term->name;
// SHOW POST COUNT
echo $term->count;
echo '</li>';
}
echo '</ul>';
}
2
Answers
Answer based @amarinediary solution. A new WP_Query with
date_query
did the trick:We can create a custom function, querying our current taxonomy term and specifying a
date_query
after
attribute.On the front-end we can call our custom function
get_posts_tally_weekly()
. We can specify the$post_type
,$post_status
,$taxonomy
and$term
.From a taxonomy page, to retrieve the weekly posts count: