admin管理员组

文章数量:1122846

function test_dashboard_content() {
    $args = array( 'posts_per_page' => -1, 'post_type' => array( 'post', 'page' ),
    'post_status' => 'publish', 'date_query' => array( 'after' => '1 year ago' ) );
    $posts = get_posts( $args );
    $testlist = '';
    foreach ( $posts as $post ) {
        $testlist .= '{date: "' . get_the_date( 'Y-n-j', $post ) . '", value: "1"},';
    }
}

Right now, the value (value: "1") is simply hard-coded as 1, but how would I collect the total post count for each respective get_the_date instead in this context?

function test_dashboard_content() {
    $args = array( 'posts_per_page' => -1, 'post_type' => array( 'post', 'page' ),
    'post_status' => 'publish', 'date_query' => array( 'after' => '1 year ago' ) );
    $posts = get_posts( $args );
    $testlist = '';
    foreach ( $posts as $post ) {
        $testlist .= '{date: "' . get_the_date( 'Y-n-j', $post ) . '", value: "1"},';
    }
}

Right now, the value (value: "1") is simply hard-coded as 1, but how would I collect the total post count for each respective get_the_date instead in this context?

Share Improve this question asked May 8, 2024 at 12:01 WPdummyWPdummy 235 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Instead of the second loop, do the following:

  1. Count the posts per date

  2. Display them as you will

    // Count the posts per date
    $count = [];
    
    foreach ( $posts as $post ) {
    
        $date = get_the_date( 'Y-n-j', $post );
    
        $count[ $date ] =  ! isset( $count[ $date ] ) ? 1 : $count[ $date ] + 1;
    
    }
    

本文标签: loopHow to get total posts count for each date