jon-c
2/23/2017 - 8:34 PM

gistfile1.txt

public function prepare() {

		$data = [];
		$total = 0;
		// Get the regions
		$regions = get_terms( array(
		    'taxonomy' => 'naspd_company_region',
		    'hide_empty' => true,
		) );
		foreach ( $regions as $region ) {
			$companies_args = array(
				'post_type' => 'naspd_company',
				'tax_query' => array(
					'taxonomy'  => 'naspd_company_region',
					'field'     => 'term_id',
					'terms'     => $region->term_id,
				),
				'orderby'   => 'title',
				'order'     => 'ASC',
			);
			// Get the companies in the region
			$companies_query = new \WP_Query( $companies_args );
			$companies = [];
			foreach ( $companies_query->posts as $company ) {
				$company_address_city = get_post_meta( $company->ID,'naspd_address_city', true );
				$company_address_state = get_post_meta( $company->ID,'naspd_address_state', true );
				$company_address_country = get_post_meta( $company->ID,'naspd_address_country', true );
				$company_address = $company_address_city . ', ' . $company_address_state . ' ' . $company_address_country;
				// Add the company
				$companies[] = array(
					'title' => $company->post_title,
					'items' => array(
						'title' => $company_address,
					),
				);
			}
			$region_total = count( $companies );
			$total = $total + $region_total;
			// Add the region
			$data['items'][] = array(
				'title'     => $region->name,
				'items'      => $companies,
				'footer'    => 'Total for ' . $region->name . ' Region: ' . $region_total,
			);
		}
		$data['footer'] = 'Total for All Regions: ' . $total;
		return $data;
	}