skip to Main Content

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


  1. Chosen as BEST ANSWER

    Answer based @amarinediary solution. A new WP_Query with date_query did the trick:

    // SHOW POST COUNT
    $post_args = array(
        'post_type' => 'guided_lessons',
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy,
                'field'    => 'id',
                'terms'    => $child_term,
            ),
        ),
        // Checking for the date here
        'date_query' => array(
            array(
                'after' => '1 week ago',
            ),
        ),
    );
    $post_query = new WP_Query( $post_args );
    $new_posts = $post_query->found_posts;
    echo $new_posts;
    

  2. We can create a custom function, querying our current taxonomy term and specifying a date_query after attribute.

    /**
     * https://stackoverflow.com/a/66780352/3645650
     * `get_posts_tally_weekly( $post_type = 'post', $post_status = 'any', $taxonomy = 'category', $term = 'uncategorized' );`
     * @return Integer Return a weekly posts tally.
     * @param String $post_type (Optional) Post type to query. Default to 'post'.
     * @param String $post_status (Optional) Post status to query. Default to 'any'.
     * @param String $taxonomy (Optional) Taxonomy to query. Default to ''.
     * @param String $term (Optional) Term to query. Default to ''.
     */
    function get_posts_tally_weekly( $post_type = 'post', $post_status = 'any', $taxonomy = 'category', $term = 'uncategorized' ) {
        $query = new WP_Query( [
            'post_type' => $post_type,
            'post_status' => $post_status,
            'orderby' => 'date',
            'order' => 'DESC',
            $taxonomy => $term,
            'date_query' => [
                'after' => '1 week ago'
            ],
        ] );
        return $query->found_posts;
        wp_reset_postdata();
    };
    

    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:

    <?= get_posts_tally_weekly( 'custom-post-type-slug', 'publish', get_queried_object()->taxonomy, get_queried_object()->slug ); ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search