对数组的值进行汇总或排序

问题描述

使用以下代码,我可以得到各个预订的总人数。但是,我希望根据开始日期汇总值,而不是所有预订的总和。

我需要每天的总数。所以开始日期相同的值。不是所有天的总人数。

    $total_count = 0; // Initializing
                    
                    
        $reservation_ids = get_posts( array(
            'posts_per_page' => -1,'post_type'   => 'arb_reservation','post_status' => 'paid','start_date'  => 'start_date','fields'      => 'ids',) );
                    
                    
                    // Loop through reservation Ids
    foreach ( $reservation_ids as $reservation_id ) {
   $reservation = new ARB_Reservation( $reservation_id );
    $count   = array_sum( $reservation->get_person_counts() );

    $total_count += $count;

}

解决方法

你可以包容给你计数和WP_Query,见下图:

// define target date
$today = date( 'Y-m-d' );

// build query
$args = array(
    'posts_per_page' => -1,'post_type' => 'arb_reservation','post_status' => 'paid','date_query' => array(
        array( 
            'after' => $today,'before' => $today,'inclusive'  => true,)
    ),);
$query = new WP_Query( $args );

// example count
echo count( $query->have_posts() );