phpwordpresswoocommercewoocommerce-bookings

Sumup or order values of an array


with the following code I get the total persons of the individual reservations. However, I would like to have the values ​​summarized depending on the start date ,and not as a total of all reservations.

I need the total amount of persons per day. So the values ​​for which the start date is the same. Not the total persons of all days.

    $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;

}

Solution

  • You can you inclusive to give you the count and WP_Query, see below:

    // 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() );